简体   繁体   中英

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?

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];

只是一个带有现代 Obj-C 语法的 iOS7 更新:

[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
UITextAttributeTextColor //Is deprecated on iOS 7. 

此代码用于从外观代理更改文本颜色。

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];

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)

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];

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;

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

Swift 4.2

let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: nil)
doneButton.setTitleTextAttributes([.foregroundColor: UIColor.red], for: .normal)

Swift 4.2

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")
    }

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];

如果您需要设置不同于默认外观的颜色,请使用:

barButtonItem.tintColor = .red

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM