Reputation: 653
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?
Upvotes: 33
Views: 39036
Reputation: 643
If you need to set a color different from the default appearance, use:
barButtonItem.tintColor = .red
Upvotes: 2
Reputation: 1331
UIBarButtonItem using attributed text:
func createCancelButton() {
guard let font = UIFont(name: "OpenSans", size: 12) else { return }
let cancelButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(cancelTapped))
cancelButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.blue, NSAttributedString.Key.font : font], for: .normal)
navigationItem.leftBarButtonItem = cancelButton
}
@objc func cancelTapped() {
print("cancelTapped")
}
UIBarButtonItem using plain text:
func createCancelButton() {
let cancelButton = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(cancelTapped))
cancelButton.tintColor = UIColor.blue
navigationItem.leftBarButtonItem = cancelButton
}
@objc func cancelTapped() {
print("cancelTapped")
}
Upvotes: 3
Reputation: 4739
Swift 4.2
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: nil)
doneButton.setTitleTextAttributes([.foregroundColor: UIColor.red], for: .normal)
Upvotes: 4
Reputation: 465
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)
Upvotes: 9
Reputation: 1571
The main thing that everyone should do if it's not your project and you just need to add some changes - is check
[UIBarButtonItem appearance]
I wasted a lot of time to realise that someone set wrong appearance of UIBarButtonItem
Upvotes: 0
Reputation: 1976
Old question, here's the swift 2.2 solution:
let cancel = UIBarButtonItem(title: "CANCEL", style: .Bordered, target: self, action: #selector(goToPreviousView))
cancel.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Normal)
self.navigationItem.leftBarButtonItem = cancel
Upvotes: 3
Reputation: 9355
Just an iOS7 Update with Modern Obj-C Syntax:
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
Upvotes: 33
Reputation: 159
UITextAttributeTextColor //Is deprecated on iOS 7.
Set the color of BarButtonItem in a way like this
[_barButtonItem setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:250/255.0
green:240/255.0
blue:230/255.0
alpha:1.0],
NSForegroundColorAttributeName,nil]
forState:UIControlStateNormal];
Upvotes: 1
Reputation: 385
UITextAttributeTextColor //Is deprecated on iOS 7.
This code is used for change the text color from from appearance proxy.
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
Upvotes: 13
Reputation: 1409
this code is used for change the text color of the UIBarButtonItem on the navigation bar:
UILabel *lblTotCaratteri = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 25)];
lblTotCaratteri.textAlignment = UITextAlignmentCenter;
lblTotCaratteri.font = [UIFont italicSystemFontOfSize:13.0];
lblTotCaratteri.textColor = [UIColor redColor];
lblTotCaratteri.backgroundColor = [UIColor clearColor];
lblTotCaratteri.adjustsFontSizeToFitWidth = YES;
lblTotCaratteri.text = @"Cancel";
UIBarButtonItem *lblCaratteri = [[UIBarButtonItem alloc] initWithCustomView: lblTotCaratteri];
self.navigationItem.rightBarButtonItem = lblCaratteri;
Upvotes: 4
Reputation: 7226
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];
Upvotes: 7
Reputation: 7226
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];
Upvotes: 107