为 iOS 7 构建时,我的自定义字体正确显示在 UILabel 中。但是,使用 XCode 6 和 iOS 8 构建时会显示另一种“标准”字体。
代码:
UILabel *label = [[UILabel alloc] initWithFrame:rect];
UIFont *font = [[CAPTheme sharedTheme] appTitleFont];
label.text = title;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.alpha = kLabelAlphaConstant;
[label setFont:font];
...
- (UIFont *)appTitleFont
{
NSArray *familyNames = [UIFont familyNames];
for (NSString *aFamilyName in familyNames) {
NSArray *fontNames = [UIFont fontNamesForFamilyName:aFamilyName];
NSLog(@"familyname: %@", aFamilyName);
for (NSString *aFontName in fontNames) {
NSLog(@" %@", aFontName);
}
}
return [UIFont fontWithFamily:@"MyFont" size:15.0f];
}
...
+ (UIFont*)fontWithFamily:(NSString*)fontFamily size:(float)size
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
return [UIFont fontWithName:fontFamily size:size];
}
else {
UIFont *font = [UIFont fontWithName:fontFamily size:size];
UIFontDescriptor *des = [[font fontDescriptor] fontDescriptorByAddingAttributes:@{
UIFontDescriptorTextStyleAttribute: UIFontTextStyleHeadline,
UIFontDescriptorSizeAttribute: @(size)
}];
UIFont *finalFont = [UIFont fontWithDescriptor:des size:0.0];
return finalFont;
}
}
让我们称我的字体为“MyFont”。然后它在调试打印中显示为:
familyname: MyFont
MyFont
MyFont-Bold
我已经尝试将其添加为 ttf 和 otf 文件。它被添加到 plist 文件中的 Fonts provided by application
下。它也被添加到目标中。我也尝试过只使用其中一种字体。
有趣的是,我有根据标签大小调整标签大小的代码。当输入不存在的字体系列名称(即“MyFont123”)时,文本将根本不显示,这意味着它确实识别出自定义字体存在,但它不会显示它。
有什么想法吗?
这是我创建的演示项目来展示问题:https://www.dropbox.com/s/mkn7xb3fb2lmh20/customLabel.zip?dl=0 使用 iOS 8 运行它,字体将不会显示。
编辑:我的猜测是我不能将 fontDescriptors 与自定义字体一起使用,但后来我又回到原来的问题,我在 iOS 8 上崩溃并出现此错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'scaledValueForValue: called on a font that doesn't have a text style set'
与此类似scaledValueForValue: called on a font that doesn't have a text style set
最佳答案
您是否已将这些文件添加到您的 Info.plist 文件中?
我也建议您阅读这篇文章。
http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
关于ios - iOS 8 中的自定义字体未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26239296/