1

I'm using NSDictionary to change the appearance of UIBarButtonItem in the appDelegate file:

UIBarButtonItem *barButtonItemProxy = [UIBarButtonItem appearanceWhenContainedIn:
                                       [UINavigationBar class], [UINavigationController class], nil];

NSDictionary *textAttributes = @{UITextAttributeFont :
                                     [UIFont fontWithName:@"ChocoBold" size:13.0f],
                                 UITextAttributeTextColor : [UIColor whiteColor],
                                 UITextAttributeTextShadowColor : [UIColor blackColor],
                                 UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(0.0f, -1.0f)]
                                 };
[barButtonItemProxy setTitleTextAttributes:textAttributes forState:UIControlStateNormal];

The app works fine in the simulator but when I run it on a device the app crashes with the following exception:

 [__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]

The crash happens in NSDictionary *textAttributes line.

I don't understand which parameter is nil in that dictionary?

oridahan
  • 574
  • 1
  • 7
  • 20
  • 1
    Log `[UIFont fontWithName:@"ChocoBold" size:13.0f]` – Shashank Mar 09 '13 at 17:05
  • Do you have `ChocoBold` font in your project? If not try adding it. – tolgamorf Mar 09 '13 at 17:09
  • I logged `UIFont fontWithName:@"ChocoBold" size:13.0f]` and it shows null. I have ChocoBold added to my project, I added it to the plist under "Fonts provided by application" and added it to the Build Phases. Also, I'm using it across my app and it works fine. – oridahan Mar 09 '13 at 17:18
  • Did you add the file to the target? – Ares Mar 09 '13 at 17:35
  • Deleting the files and adding them to the project again fixed it. @Ares Yes it was under "Copy Bundle Resources", I don't know why it didn't work. – oridahan Mar 09 '13 at 17:38

1 Answers1

4
NSLog(@"font family %@",[UIFont fontNamesForFamilyName:@"Choco"]);

If choco font family exists in your app than it will log all the available font names. Then copy the exact font name. May be the font name you are using is wrong e.g. its Choco-Bold instead of chocobold etc.

NSMutableDictionary *textAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       [UIColor whiteColor],UITextAttributeTextColor,
                                       [UIColor blackColor],UITextAttributeTextShadowColor,
                                       [NSValue valueWithUIOffset:UIOffsetMake(0.0f, -1.0f)],UITextAttributeTextShadowOffset,
                                       [UIFont fontWithName:@"Helvetica" size:13.0f],UITextAttributeFont,nil];

Try with the "Helvetica" font if it works then the problem is with your font.

Muhammad Zeeshan
  • 2,441
  • 22
  • 33
  • The problem was with the font, but I have no idea what caused it. After deleting the file and adding them again it works fine. – oridahan Mar 09 '13 at 17:46
  • For me, worked the answer of Zeeshan. I used the name provided by the nslog and worked fine (i had changed the name of the font file, before, and used that in the app code and in the info Plist, but changing the filename seems not to be enough) – Sasha Grievus Apr 14 '15 at 17:58