Reputation: 1616
I'm trying to change the font color of the text on my back button in my UINavigationControllerBar
:
[[UIBarButtonItem appearance] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
I get this error:
[_UIBarItemAppearance setTitleColor:forState:]: unrecognized selector sent to instance 0x69aeb70'
Upvotes: 27
Views: 16216
Reputation: 4636
Use this instead, default function available in iOS 5:
UIBarButtonItem *backbutton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil];
[backbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor],UITextAttributeTextColor,
[UIFont fontWithName:TEXTFONT size:16.0f],UITextAttributeFont,nil]
forState:UIControlStateNormal];
Upvotes: 15
Reputation: 7521
Solution in Swift 4:
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedStringKey.font: UIFont(name: "MyriadPro-SemiboldCond", size: 16)!,
NSAttributedStringKey.foregroundColor: UIColor.white
], for: .normal)
Add this in AppDelegate and it'll be applied to all buttons in the app.
Upvotes: 3
Reputation: 1604
And the beautiful solution, iOS7+ (because of attribute names):
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor: [UIColor colorWithWhite:0.0f alpha:0.750f]];
[shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)];
[[UIBarButtonItem appearance] setTitleTextAttributes:@{
NSFontAttributeName: [UIFont systemFontOfSize:24],
NSForegroundColorAttributeName: [UIColor colorWithWhite:0.2 alpha:1.0],
NSShadowAttributeName: shadow,
} forState:UIControlStateNormal];
Upvotes: 6
Reputation: 2181
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:kDefaultFont size:16.0f],UITextAttributeFont,
nil] forState:UIControlStateNormal];
Upvotes: 18
Reputation: 1616
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
[attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
seems to work!
Upvotes: 50