分配自定义字体

时间:2017-06-14 05:37:44

标签: ios objective-c cocoa-touch fonts

我正在尝试在我的应用中添加自定义字体GothamPro-NarrowMedium 我在我的项目中添加了.otf文件,并在info.plist中添加了应用程序提供的字体并添加到目标成员资格中。现在,当我通过界面分配字体时,当我尝试在NSMutableAttributedString中添加字体时,它可以正常工作我得到以下异常

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:' * - [__ NSPlaceholderDictionary   initWithObjects:forKeys:count:]:尝试从中插入nil对象   对象[0]”   * 第一次抛出调用堆栈:(0x181ad6fe0 0x180538538 0x1819bd9b4 0x1819bd824 0x100075dac 0x100073fa8 0x187c07ec0 0x187c07a9c   0x187f975f0 0x187f5bce0 0x187f58130 0x187e94950 0x187e869ec   0x187bfa648 0x181a849a8 0x181a82630 0x181a82a7c 0x1819b2da4   0x18341c074 0x187c6d058 0x10011ff44 0x1809c159c)libc ++ abi.dylib:   以NSException类型的未捕获异常终止

我的代码是

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.alignment = _lbl_mylabel.textAlignment;

    NSDictionary *attribs = @{
                              NSForegroundColorAttributeName: self.lbl_ticketdetail.textColor,
                              NSFontAttributeName: self.lbl_ticketdetail.font
                              };
    NSMutableAttributedString *attributedText =
    [[NSMutableAttributedString alloc] initWithString:text
                                           attributes:attribs];
paragraphStyle.lineSpacing = 3;

        [attributedText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"GothamPro-NarrowMedium" size:20.0],NSParagraphStyleAttributeName:paragraphStyle}
                                range:cmp];

        NSRange plce = [text rangeOfString:place];
        [attributedText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"GothamPro-NarrowMedium" size:16.0],NSParagraphStyleAttributeName:paragraphStyle}
                                range:plce];

        NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle1.lineSpacing = 1;
        NSRange tkt_num_range = [text rangeOfString:STR_tkt_num_club];
        [attributedText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"GothamMedium" size:13.0],NSParagraphStyleAttributeName:paragraphStyle1} range:tkt_num_range];
self.mylabel.attributedText = attributedText;

1 个答案:

答案 0 :(得分:0)

由于尝试将null对象插入Dictionary而发生崩溃。

我认为您在为项目添加自定义字体时遇到问题。

请重新检查

  
      
  1. 在您的应用中添加.TTF字体。
  2.   
  3. 将密钥 Fonts provided by application 添加到新行中   添加每个.TTF文件名。
  4.   

完成此步骤后,我猜您的info.plist可能会这样:

<key>UIAppFonts</key>
<array>
    <string>GothamMedium</string>
    <string>GothamPro-NarrowMedium</string>
</array>
  

当我通过Storyboard设置自定义字体时,不需要上述步骤   对我来说!