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

在为iOS 7构build时,我的自定义字体正确显示在UILabel中。 但是,使用XCode 6和iOS 8构build另一个“标准”字体会显示出来。

码:

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”。 然后在debugging打印中显示如下:

 familyname: MyFont MyFont MyFont-Bold 

我已经尝试将它添加为ttf和otf文件。 它被添加Fonts provided by application plist文件中的Fonts provided by application下。 它也被添加到目标。 我也试过只使用其中一种字体。

有趣的是,我有代码,根据标签大小调整标签的大小。 input不存在的字体系列名称(即“MyFont123”)将使文本根本不显示,这意味着它确实识别自定义字体存在,但它不会显示它。

有任何想法吗?

这里是我创build的演示项目,以显示问题: 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:调用没有文本样式集的字体

你有没有添加文件到你的Info.plist文件呢?

我build议阅读这篇文章。

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