添加自定义字体到一个酒吧按钮项目

问题描述:

我正在尝试添加自定义字体到一个酒吧按钮项目,但我得到这个崩溃。添加自定义字体到一个酒吧按钮项目

-[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]' 
*** First throw call stack: 

我敢肯定,我已经正确添加的字体,它在我的info.plist和自定义字体在IB显示出来。

这是我如何设置字体:

NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"OpenSans-Regular" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]}; 
    [[UIBarButtonItem appearance]setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal]; 

任何想法,为什么我收到这个错误?

这就是我如何添加字体。

enter image description here

enter image description hereenter image description hereenter image description here

+0

是否问题是否仍然存在,如果你会使用'取代

NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"OpenSans-Regular" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]}; 

? – Azat 2015-03-30 21:24:04

+0

我很确定这个问题与我的自定义字体有关。我将字体名称更改为@“HelveticaNeue-Medium”,应用程序运行良好并更改了字体。所以它与如何实现自定义字体......不知道我在做什么错了。 @Azat – Andy 2015-03-30 21:26:06

+0

由于您没有提供有关如何安装字体的详细信息,将它添加到Info.plist中,并验证了真实的字体名称,没有人能够帮助您。 – rmaddy 2015-03-30 21:55:07

要自定义字体添加到您的应用程序,这里是一个有用的指南:

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

您可以使用此教程,双精度检查你的自定义字体。

无论您使用哪种方法添加自定义字体,运行下面的代码以检查字体在您的应用中加载时的名称。它会列出您的应用中可用的所有字体。

for (NSString* family in [UIFont familyNames]) 
{ 
    NSLog(@"%@", family); 

    for (NSString* name in [UIFont fontNamesForFamilyName: family]) 
    { 
     NSLog(@" %@", name); 
    } 
} 

然后,您可以在系统中获取字体的名称。

我不知道如何添加自定义字体,但通常情况下,常规字体将加载没有常规后缀。 [:12 UIFont systemFontOfSize]`,而不是自定义的

所以,用

NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"OpenSans" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]}; 
+0

谢谢,我实际上完全按照该教程进行操作,并且在记录字体名称时我的自定义字体不会显示出来..... argh。 – Andy 2015-03-30 21:56:39

+0

@Andy字体名称很少与文件名完全匹配。再次扫描日志输出以查找字体。一次测试,删除字体条目并再次运行此代码,看看输出是否有任何不同。 – rmaddy 2015-03-30 22:11:04