使用自定义字体而不包括在info.plist ios中
在iOS中,如果我们想要使用自定义字体,我们必须在应用程序包中包含font.ttf
,并将font.ttf
作为值添加到info.plist文件中的font键。
我想使用自定义字体,在设备中安装应用程序后,将从我的服务器下载其ttf文件。
1:是否可以在我的应用程序中使用该字体
2:如果是,我该如何使用该字体?
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
声明UIAppFonts
。
资源
注意:您应该包含CoreText
以使CTFontManagerRegisterGraphicsFont
可用。