0

I am trying to add custom font in my application GothamPro-NarrowMedium I added the .otf file in my project and in info.plist I added Fonts provided by the application and added to target membership. Now when I assign the font through Interface it works perfectly when I try to add it in NSMutableAttributedString I get the below exception

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]' * First throw call stack: (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: terminating with uncaught exception of type NSException

My code is

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;
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
AJ Sanjay
  • 1,276
  • 1
  • 12
  • 29
  • 1
    ***you are trying to put nil in the dictionary (which is not allowed). Since you are building the dictionaries with string literals those can't be nil.*** – Anbu.Karthik Jun 14 '17 at 05:39
  • i think `self.lbl_ticketdetail.textColor` or `self.lbl_ticketdetail.font` would be `NIL`. Please check the values. Attribute dictionary shouldn't accept `NIL` – Lal Krishna Jun 14 '17 at 05:49
  • @Anbu.Karthik As I had mentioned The font is properly added in .Plist – AJ Sanjay Jun 14 '17 at 05:50
  • @LalKrishna If I use another font name it works so I think that is not the issue – AJ Sanjay Jun 14 '17 at 05:51
  • sorry, your reply is unclear. – Lal Krishna Jun 14 '17 at 05:57
  • If when you use other font name, it works, but not this one hence it probably means your font name is wrong. – GeneCode Jun 14 '17 at 06:01
  • try to convert your font .otf to ttf . from here https://onlinefontconverter.com/ another solution : set set target to that font . – KKRocks Jun 14 '17 at 06:04
  • @AJSanjay please try with UIFont fontWithName:@"Gotham-ProNarrowMedium" size:16.0],NSParagraphStyleAttributeName:paragraphStyle and see whether it is working ? – Aravind A R Jun 14 '17 at 06:05
  • @AravindAR Gotham-ProNarrowMedium is also not working – AJ Sanjay Jun 14 '17 at 06:35
  • @LalKrishna [attributedText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"GothamBold" size:16.0],NSParagraphStyleAttributeName:paragraphStyle} range:plce]; - >This code works and [attributedText setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"GothamPro-NarrowMedium" size:16.0],NSParagraphStyleAttributeName:paragraphStyle} range:plce]; - > this not works – AJ Sanjay Jun 14 '17 at 06:37
  • @GeneCode gotham-pro-narrow-medium.otf is the font I downloaded and imported to my code – AJ Sanjay Jun 14 '17 at 06:38
  • @KKRocks What you told is irrelevant – AJ Sanjay Jun 14 '17 at 06:40
  • Basically the issue here is you are adding a nil object to a dictionary. It may be the font you are using. Have you seen the font name of it using `font book` I think I encountered the same issue where in the filename was different from the font name or something like that – Joshua Jun 14 '17 at 06:48
  • Your issue is about the font: https://stackoverflow.com/questions/15984937/adding-custom-fonts-to-ios-app-finding-their-real-names https://stackoverflow.com/questions/26667951/how-to-use-custom-font-in-ios-eg-helvetica-cy-ttf-with-xcode-programaticall etc. – Larme Jun 14 '17 at 08:41
  • i think you used `GothamPro-NarrowMedium` & `GothamMedium` (i mean the custom fonts you imported) programatically **only**. Are you? – Lal Krishna Jun 14 '17 at 09:16

1 Answers1

1

Crash occurred due to Attempting to insert null object to Dictionary.

I think you have problem with adding custom font to your project.

Please re-check

  1. Add .TTF font in your application.
  2. Add the key Fonts provided by application to a new row and add each .TTF file names.

After this steps, I guess your info.plist may like this:

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

To find the Font PostScript name:

  • Open Font book
  • Select Font
  • View -> Show Font Info ( + I)
  • Copy the PostScript name
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84