Using custom font without including in info.plist ios

戏子无情 提交于 2019-12-08 07:26:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/30412068/using-custom-font-without-including-in-info-plist-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!