在我的应用程序中,我想使用文档目录中的 ttf。通过参考我使用下面代码的链接来使用来自 CGFontRef 的字体
+ (UIFont *)loadFontAtPath:(NSString*)path{
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
// path is "/Users/............./Documents/bloktilt.ttf"
if(data == nil){
NSLog(@"Failed to load font. Data at path is null");
return nil;
}
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGFontRef font = CGFontCreateWithDataProvider(provider);
NSString *fontName = (NSString *)CGFontCopyPostScriptName(font);
UIFont *font1 = [UIFont fontWithName:fontName size:15];
if(!CTFontManagerRegisterGraphicsFont(font, &error)){
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
return font1;
}
但问题是上面的代码返回 nil