gpt4 book ai didi

iphone - iOS 中的动态自定义字体加载器

转载 作者:可可西里 更新时间:2023-11-01 06:18:06 24 4
gpt4 key购买 nike

我已经知道如何在 iPhone 应用程序中将自定义字体加载到我的项目中,来自 here我想问一下是否有办法从代码中做到这一点?我的问题是我的应用程序中有一个资源文件夹,我有一个字体文件名,我们称之为“myfont.ttf”。

我想获取一个 ttf 文件并将其从代码 放入 plist 文件,而且我还想知道 fontWithName:size: 方法的显示名称。有办法实现吗?

最佳答案

这是一个较老的问题,但这里有一种方法可以解决这个问题,以防其他人遇到这个问题。


+ (void)loadFontAtPath:(NSString*)path{
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
if(data == nil){
NSLog(@"Failed to load font. Data at path is null");
return;
}
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if(!CTFontManagerRegisterGraphicsFont(font, &error)){
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
}

这将在运行时指定的路径加载字体,然后您可以像平常一样使用它而无需将其添加到 plist。

关于iphone - iOS 中的动态自定义字体加载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312401/

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