How can I change the font color of a UIBarButton item?
34,247
Solution 1
This question was answered here. Basically, you have to create a UIButton, configure it as you wish, and then initialize the Bar Button Item with the UIButton as a custom view.
Solution 2
I found a simpler way to just change the color of title: iOS7:
UIBarButtonItem *button =
[[UIBarButtonItem alloc] initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[button setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], NSForegroundColorAttributeName,nil]
forState:UIControlStateNormal];
and prior iOS7:
UIBarButtonItem *button =
[[UIBarButtonItem alloc] initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[button setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], UITextAttributeTextColor,nil]
forState:UIControlStateNormal];
Solution 3
There are Two ways to change the color of the Text displayed UIBarButtonITem.
1)
[barButtonITem setTintColor:[UIColor redColor]];
2) or you can you any UIColor class method. this is really efficient solution.Althought for iOS 7 you can use NSForegroundColorAttributeName and can use
barButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
Solution 4
On similar note from Guvvy Ava you can change font size like
[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:26.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
Solution 5
swift version 4.0 or later
barButtonITem.tintColor = UIColor.red // for textColor change
if want to change font as well as text color use the following code
barButtonITem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor :UIColor.red,NSAttributedStringKey.font:UIFont.init(name:"Avenir", size:15.0)], for: UIControlState.normal)
Related videos on Youtube
Author by
Sheehan Alam
iOS, Android and Mac Developer. i can divide by zero.
Updated on April 10, 2020Comments
-
Sheehan Alam about 4 years
I want to change the color to red.
-
memmons over 12 yearspossible duplicate of UIBarButtonItem with color?
-
-
gschandler almost 12 yearsCertainly a good solution if you are iOS 5.0 or greater. OP: Check out UIBarItem.h for details.
-
AdamM almost 12 yearsI have been searching for ages to find out how to change text color, thanks a lot!!. Luckily majority of phones are on IOS5 now so this is not really a concern
-
Mirko Brunner about 10 yearsi have fixed this answer for iOS7
-
Robert J. Clegg about 10 yearsWorks in iOS7. Thanks dude!
-
Anand Gautam almost 10 yearsThanks @Gavy, it's working fine. Can you tell me how to change the font of title?
-
Gavy almost 10 years@AnandGautam You can use the UITextAttributeFont attribute to change the font.