Reputation: 17169
I am using a little chunk of code like below to change the text attributes of my nav bar title app wide and it works great.
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Cochin-BoldItalic" size:0.0], UITextAttributeFont,
nil]];
But I want to be able to just as easily do this for UIBarButtonItem text as well but I can't figure it out as it doesn't share the same or similar methods it appears.
Tried this code, not making any changes to text:
[[UIBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Cochin-BoldItalic" size:12.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
Upvotes: 0
Views: 5272
Reputation: 3308
This seems to work on iOS 7.1:
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:[UIFont fontWithName:@"Resamitz" size:16.0]}
forState:UIControlStateNormal];
Upvotes: 0
Reputation: 11320
You want to use the - (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state
method of UIBarItem (UIBarButtonItem inherits from UIBarItem).
Check out the docs for more details: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarItem_Class/Reference/Reference.html#//apple_ref/occ/cl/UIBarItem
Try this:
//Suppose you have initialized barButton elsewhere`
[barButton setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Cochin-BoldItalic" size:12.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
Upvotes: 10