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>}))
Error code 105 means that the font is already registered (ref: https://developer.apple.com/documentation/coretext/ctfontmanagererror/alreadyregistered)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With