Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the font color / Text Color of the UIBarButtonItem on navigation bar

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?

like image 418
user1645721 Avatar asked Sep 06 '12 07:09

user1645721


4 Answers

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];
like image 188
IronManGill Avatar answered Nov 12 '22 15:11

IronManGill


Just an iOS7 Update with Modern Obj-C Syntax:

[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
like image 40
GangstaGraham Avatar answered Nov 12 '22 16:11

GangstaGraham


UITextAttributeTextColor //Is deprecated on iOS 7. 

This code is used for change the text color from from appearance proxy.

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
like image 13
miguelghz Avatar answered Nov 12 '22 16:11

miguelghz


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)
like image 9
Saggy Avatar answered Nov 12 '22 15:11

Saggy