How to change the font color / Text Color of the UIBarButtonItem on navigation bar
37,723
Solution 1
Another method is :-
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[button setTitle:@"Delete" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[button.layer setCornerRadius:4.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor grayColor] CGColor]];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(batchDelete) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* deleteItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Solution 2
Check this out :-
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:nil action:nil];
[cancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
Solution 3
Just an iOS7 Update with Modern Obj-C Syntax:
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
Solution 4
UITextAttributeTextColor //Is deprecated on iOS 7.
This code is used for change the text color from from appearance proxy.
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
Solution 5
Here is updated swift 4.0 version code :
let reset = UIBarButtonItem(title: "Reset All", style: .plain , target: self, action: #selector(self.resetButtonClicked(_ :) ))
reset.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .normal)
Author by
user1645721
Updated on October 22, 2020Comments
-
user1645721 over 3 years
I add a bar button to the navigation bar programitically as follows
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"CANCEL" style:UIBarButtonItemStyleBordered target:self action:@selector(goToPreviousView)]; self.navigationItem.leftBarButtonItem = cancel;
Now I want to display Text "CANCEL" in RED Color.
I mean that I need to change the text on the bar button items, but not the tint color of the button.
How to do that?
-
user1645721 over 11 yearsit causes to crash: Thread 1: EXC_BAD_ACESS (code=2 address=0x0)
-
Asta ni enohpi about 11 years+1 it works perfect .thanks .@ user1645721 setTitleTextAttributes function available from IOS 5
-
Lukasz over 10 yearsThis sets background color, he asked for text color.
-
Mark Adams about 10 yearsIt should be noted that
UITextAttributeTextColor
was replaced withNSForegroundColor
in 7.0. -
Gene Z. Ragan over 8 yearsNSForegroundColorAttributeName is the correct replacement for UITextAttributeTextColor
-
Bogdan about 7 yearsUse NSForegroundColorAttributeName instead of UITextAttributeTextColor