In iOS if we want to use a custom font we have to include the font.ttf
in app bundle and add the font.ttf
as a value to font key in the info.plist file.
I want to use a custom font whose ttf file will be downloaded from my server after the application is installed in the device.
1: Is it possible to use that font in my application
2: If yes how can I use that font?
NSData *inData = /* your decrypted font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef fontRef = CGFontCreateWithDataProvider(provider);
if (!CTFontManagerRegisterGraphicsFont(fontRef, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
} else {
CFStringRef fontNameRef = CGFontCopyPostScriptName(fontRef);
UIFont *font = [UIFont fontWithName:(__bridge NSString *)fontNameRef size:someSize];
// Your UIFont is ready.
CFRelease(fontNameRef);
}
CFRelease(fontRef);
CFRelease(provider);
You can load a font this way at any time during your app’s execution, and it immediately becomes available in UIFont
and UIWebView
(via regular font-family
CSS declarations, no @font-face
required) just as if it had been declared in UIAppFonts
.
Source
Note: You should include CoreText
to have CTFontManagerRegisterGraphicsFont
be available.