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

标签 ios objective-c fonts 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];

...

- (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/

相关文章:

ios - "Parse Issue Expected ' ] '"

ios - 如何使用用 NSString 编写的表达式查询 NSDictionary?

objective-c - 在 iOS 中检测 PAN 手势的方向

objective-c - 实现 WebView 数据库配额委托(delegate)

css - Roboto Condensed google 字体在所有浏览器中都会回落

ios - 如何在上传前在 iOS Share Extension 中缩小图像

ios - 如何在 didSelectRow 之后关闭 UISearchController?

objective-c - 管理 NSSearchfield 上的键盘事件,无需子类化

html - 是否可以使用 Font Awesome 图标来选择默认的下拉符号

iphone - lineHeight 和领先之间有什么区别?