iOS 8中的自定义字体不显示 [英] Custom fonts in iOS 8 not showing

查看:192
本文介绍了iOS 8中的自定义字体不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构建iOS 7时,我的自定义字体正确显示在UILabel中。但是,使用XCode 6和iOS 8构建另一个标准字体。
$ b

代码:

  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:@MyFontsize:15.0f];

code











< pre $ +(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。然后显示在debug打印中:

  familyname:MyFont 
MyFont
MyFont-Bold

我已经尝试将它添加为ttf和otf文件。它被添加在由plist文件中的应用程序提供的字体下。它也被添加到目标。我也尝试过使用其中一种字体。



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



有什么想法?



下面是我创建的一个演示项目,用于显示问题: https://www.dropbox.com/s/mkn7xb3fb2lmh20/customLabel.zip?dl=0
运行它使用iOS 8和字体将不会显示。

编辑:我的猜测是,我不能使用fontDescriptors与自定义字体,但然后我回到我的原始问题,我得到了这个错误在iOS 8的崩溃:

  ***终止应用程序由于未捕获异常'NSInternalInconsistencyException' ,原因:
'scaledValueForValue:调用没有文本样式的字体集'

类似于这个 scaledValueForValue:调用没有设置文本样式的字体

解决方案

我把文件添加到Info.plist文件了吗? 'd推荐阅读这篇文章。



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


When building for iOS 7, my custom fonts are displayed in a UILabel correctly. However building with XCode 6 and for iOS 8 another "standard" font is displayed.

Code:

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;
    }
}

Let's call my font "MyFont". It is then displayed in the debug prints as:

familyname: MyFont
 MyFont
 MyFont-Bold

I have tried both to add it as ttf and otf file. It is added under Fonts provided by application in plist file. It is also added to the target. I have also tried only using one of the fonts.

Interesting is also that I have code that adjust the size of the label based on the labels size. When entering a font family name that does not exists (i.e. "MyFont123") is will make the text not show at all, meaning it does recognizes that the custom font exists, but it wont show it.

Any ideas?

Here is a demo project I created to show the problem: https://www.dropbox.com/s/mkn7xb3fb2lmh20/customLabel.zip?dl=0 run it using iOS 8 and the font will not be shown.

Edit: My guess is that I cannot use fontDescriptors with custom fonts but then I'm back to my original problem, that I get a crash on iOS 8 with this error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:  
'scaledValueForValue: called on a font that doesn't have a text style set'

Similar to this one scaledValueForValue: called on a font that doesn't have a text style set

解决方案

Have you added the files to your Info.plist file yet?

I'd recommend reading this article as well.

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

这篇关于iOS 8中的自定义字体不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆