Post

Replies

Boosts

Views

Activity

Reply to Custom Fonts in Swift Package
Update: Still no success but I was able to prove that the font is indeed inside the module I wrote a method to get available font URLs from the module like so:   static func fontNames() -> [URL] {     let bundle = Bundle.module     let filenames = ["CustomFont"]     return filenames.map { bundle.url(forResource: $0, withExtension: "ttf")! }   } Calling this method at runtime and printing the result yields this: font names: [file:///Users/davidokun/Library/Developer/CoreSimulator/Devices/AFE4ADA0-83A7-46AE-9116-7870B883DBD3/data/Containers/Bundle/Application/800AE766-FB60-4AFD-B57A-0E9F3EACCDB2/BestPackageTesting.app/BestPackage_BestPackage.bundle/CustomFont.ttf] I then tried to register the font for use in the runtime with the following method: extension UIFont {   static func register(from url: URL) {     guard let fontDataProvider = CGDataProvider(url: url as CFURL) else {       print("could not get reference to font data provider")       return     }     guard let font = CGFont(fontDataProvider) else {       print("could not get font from coregraphics")       return     }     var error: Unmanaged<CFError>?     guard CTFontManagerRegisterGraphicsFont(font, &error) else {       print("error registering font: \(error.debugDescription)")       return     }   } } When I call it like so: fontNames().forEach { UIFont.register(from: $0) } I get this error: error registering font: Optional(Swift.Unmanaged<__C.CFErrorRef>(_value: Error Domain=com.apple.CoreText.CTFontManagerErrorDomain Code=105 "Could not register the CGFont '<CGFont (0x600000627a00): CustomFont>'" UserInfo={NSDescription=Could not register the CGFont '<CGFont (0x600000627a00): CustomFont>', CTFailedCGFont=<CGFont (0x600000627a00): CustomFont>})) Any more ideas are welcome.
Jul ’20