AJ Sanjay AJ Sanjay -3 years ago 48
iOS Question

Exception while Assign Custom font

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;

Answer Source

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>

When I set custom fonts via Storyboard, above steps are NOT needed for me!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download