Jan*_*ano 37
要使用自定义字体(iOS中未包含该字体),您必须编辑<appname>-Info.plist文件并UIAppFonts使用类型数组创建新密钥,其中数组的每个元素都是一个字符串,其中包含字体文件的名称.示例:VAGRoundedStd-Light.ttf.您还必须将文件添加到项目中.

注意:当您键入UIAppFonts并按Enter键时,该键将转换为"由应用程序提供的字体".
但是,在Interface Builder(或with UIFont)中使用该字体时,不要使用字体的文件名,而是使用Mac 应用程序Font Book中打开字体时显示的名称.对于前面的例子,它将是VAG Rounded Std Light.

OS X比iOS消化TrueType格式更宽容,因此在极少数情况下,您可能会发现在OS X中有效但在iOS中无效的字体.如果发生这种情况,请更换字体以查看是否至少您已正确处理该过程.
这解决了字体许可证要求您分发加密字体的情况.
以下配方来自Marco Arment 动态加载iOS字体.它使UIFont和UIWebView中的字体可用.可以使用相同的代码从Internet加载字体.
NSData *inData = /* your decrypted 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);
Run Code Online (Sandbox Code Playgroud)
这是iOS拒绝从同一系列加载两种以上字体的情况.
这是stackoverflow用户Evadne Wu的代码解决方法.只需将以下内容粘贴到您的应用程序委托中(请注意它使用两个框架):
#import <CoreText/CoreText.h>
#import <MobileCoreServices/MobileCoreServices.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CTFontManagerRegisterFontsForURLs((__bridge CFArrayRef)((^{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *resourceURL = [[NSBundle mainBundle] resourceURL];
NSArray *resourceURLs = [fileManager contentsOfDirectoryAtURL:resourceURL includingPropertiesForKeys:nil options:0 error:nil];
return [resourceURLs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSURL *url, NSDictionary *bindings) {
CFStringRef pathExtension = (__bridge CFStringRef)[url pathExtension];
NSArray *allIdentifiers = (__bridge_transfer NSArray *)UTTypeCreateAllIdentifiersForTag(kUTTagClassFilenameExtension, pathExtension, CFSTR("public.font"));
if (![allIdentifiers count]) {
return NO;
}
CFStringRef utType = (__bridge CFStringRef)[allIdentifiers lastObject];
return (!CFStringHasPrefix(utType, CFSTR("dyn.")) && UTTypeConformsTo(utType, CFSTR("public.font")));
}]];
})()), kCTFontManagerScopeProcess, nil);
return YES;
}
Run Code Online (Sandbox Code Playgroud)
可以作为要点.作者博客评论:在iOS上从同一个字体系列中加载2+字体.
来自pixeldock.com的另一种更具参与性的解决方法:
如果添加相同字体系列的2个以上字体变体(例如"Normal","Bold"和"Extended"),则只能添加到项目中的最后两个字体变体.
如果您发现这种情况,那么这是您的SDK版本的限制,唯一的出路是使用Fontforge等字体编辑器编辑字体系列.再次,来自pixeldock.com:
| 归档时间: |
|
| 查看次数: |
9735 次 |
| 最近记录: |