iOS 8中的自定义字体未显示

Sun*_*kas 2 fonts objective-c ios ios8

在为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];
Run Code Online (Sandbox Code Playgroud)

...

- (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];
}
Run Code Online (Sandbox Code Playgroud)

...

+ (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我们打电话给我的字体"MyFont".然后它在调试打印中显示为:

familyname: MyFont
 MyFont
 MyFont-Bold
Run Code Online (Sandbox Code Playgroud)

我已经尝试将它添加为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'
Run Code Online (Sandbox Code Playgroud)

与此类似的scaledValueForValue:调用没有文本样式集的字体

Ada*_*per 9

您是否已将文件添加到Info.plist文件中?

我也建议你阅读这篇文章.

http://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/