gpt4 book ai didi

ios - 如何在 iOS 下动态加载字体。 (真正的)

转载 作者:技术小花猫 更新时间:2023-10-29 10:19:38 26 4
gpt4 key购买 nike

这个问题我已经看过很多次了,但是我还没有看到真正的答案。常见的“解决方案”是:

  • 将字体添加到应用程序包中并将其注册到 info.plist 文件中。
  • 使用自定义字体解析和渲染库(如 Zynga 的 FontLabel )。
  • 这是不可能的。

那么问题来了:iOS下如何动态加载字体?“动态”加载字体意味着加载在应用程序编译时未知的任何给定字体。

最佳答案

可以轻松地从任何位置或任何字节流动态加载字体。请参阅此处的文章:http://www.marco.org/2012/12/21/ios-dynamic-font-loading

NSData *inData = /* your font-file data */;
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (! CTFontManagerRegisterGraphicsFont(font, &error)) {
CFStringRef errorDescription = CFErrorCopyDescription(error)
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
  • 您不必将字体放入包中。
  • 您不必在 info.plist 中显式注册字体。

另见: https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CoreText_FontManager_Ref/Reference/reference.html#//apple_ref/doc/uid/TP40008278

https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGFont/Reference/reference.html#//apple_ref/c/func/CGFontCreateWithDataProvider

关于ios - 如何在 iOS 下动态加载字体。 (真正的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14048549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com