Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to register font programmatically in Swift 3

Tags:

ios

swift

I am trying to register font programmatically in swift 3. Here is the piece of code i am using to install fonts. The font i am trying to install is Oswald

func installFonts(_ fonts: [String]){
    for i in 0 ..< fonts.count{
        let font = fonts[i].components(separatedBy: ".")
        let fontPath = Bundle.main.url(forResource: font[0], withExtension: font[1])!
        var error: Unmanaged<CFError>?
        if let dataProvider = CGDataProvider(url: fontPath as CFURL) {
            let font = CGFont(dataProvider)
            if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
                print(error.debugDescription)
            }
        }
    }
}

None of the fonts get installed and error description states:

Optional(Swift.Unmanaged<__ObjC.CFError>(_value: Error Domain=com.apple.CoreText.CTFontManagerErrorDomain Code=105 \"Could not register the CGFont \'<CGFont (0x6100000d9f30): Oswald-Bold>\'\" UserInfo={NSDescription=Could not register the CGFont \'<CGFont (0x6100000d9f30): Oswald-Bold>\', CTFailedCGFont=<CGFont (0x6100000d9f30): Oswald-Bold>}))

like image 483
sachin.j Avatar asked Oct 23 '25 09:10

sachin.j


1 Answers

Error code 105 means that the font is already registered (ref: https://developer.apple.com/documentation/coretext/ctfontmanagererror/alreadyregistered)

like image 81
Dooby Avatar answered Oct 26 '25 00:10

Dooby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!