Using custom font without including in info.plist ios

我们两清 提交于 2019-12-07 13:36:28
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.

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