使用自定义字体而不包含在info.plist ios中 [英] Using custom font without including in info.plist ios

查看:33
本文介绍了使用自定义字体而不包含在info.plist ios中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS中,如果要使用自定义字体,则必须在应用程序包中包含 font.ttf ,并将 font.ttf 作为值添加到字体密钥中在 info.plist 文件中.

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.

我想使用自定义字体,将应用程序安装在设备中后,该字体的ttf文件将从我的服务器下载.

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:是否可以在我的应用程序中使用该字体

1: Is it possible to use that font in my application

2:如果可以,我该如何使用该字体?

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);

您可以在应用执行期间随时以这种方式加载字体,并且该字体会立即在 UIFont UIWebView 中可用(通过常规的 font-family CSS声明,不需要 @ font-face ),就像在 UIAppFonts 中声明过一样.

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.

来源

注意:您应该包括 CoreText ,以使 CTFontManagerRegisterGraphicsFont 可用.

Note: You should include CoreText to have CTFontManagerRegisterGraphicsFont be available.

这篇关于使用自定义字体而不包含在info.plist ios中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆