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?

5
  • 1
    Log [UIFont fontWithName:@"ChocoBold" size:13.0f] Commented Mar 9, 2013 at 17:05
  • Do you have ChocoBold font in your project? If not try adding it. Commented Mar 9, 2013 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. Commented Mar 9, 2013 at 17:18
  • Did you add the file to the target? Commented Mar 9, 2013 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. Commented Mar 9, 2013 at 17:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.