在为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文件。它被添加Fonts provided by application
到plist文件中。它还被添加到目标中。我也尝试只使用一种字体。
有趣的是,我有一些代码可以根据标签的大小来调整标签的大小。输入不存在的字体系列名称(即“ 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'
您是否已将文件添加到Info.plist文件中?
我也建议您阅读这篇文章。
http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句