30

我以编程方式向导航栏添加了一个栏按钮,如下所示

UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"CANCEL" style:UIBarButtonItemStyleBordered target:self action:@selector(goToPreviousView)];
    self.navigationItem.leftBarButtonItem = cancel;

现在我想以RED Color 显示文本“CANCEL”

我的意思是我需要更改bar button items 上的文本,而不是按钮的色调。

怎么做?

4

12 回答 12

107

看一下这个 :-

  UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:nil action:nil];
[cancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor],  UITextAttributeTextColor,nil] forState:UIControlStateNormal];
于 2012-09-06T07:55:09.907 回答
33

只是现代 Obj-C 语法的 iOS7 更新:

[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
于 2014-04-25T01:52:40.580 回答
13
UITextAttributeTextColor //Is deprecated on iOS 7. 

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

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
于 2013-09-08T17:19:37.100 回答
8

这是更新的 swift 4.0 版本代码:

let reset = UIBarButtonItem(title: "Reset All", style: .plain , target: self, action: #selector(self.resetButtonClicked(_ :) ))
reset.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .normal)
于 2017-12-18T06:04:52.523 回答
7

另一种方法是:-

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];
于 2012-09-06T07:58:25.473 回答
4

此代码用于更改导航栏上 UIBarButtonItem 的文本颜色:

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;
于 2012-09-06T09:45:59.443 回答
3

老问题,这是 swift 2.2 解决方案:

    let cancel = UIBarButtonItem(title: "CANCEL", style: .Bordered, target: self, action: #selector(goToPreviousView))
    cancel.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState: .Normal)
    self.navigationItem.leftBarButtonItem = cancel
于 2016-09-01T10:31:19.590 回答
3

斯威夫特 4.2

let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: nil)
doneButton.setTitleTextAttributes([.foregroundColor: UIColor.red], for: .normal)
于 2018-10-13T12:30:21.983 回答
2

斯威夫特 4.2

UIBarButtonItem 使用属性文本:

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 使用纯文本:

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")
    }
于 2019-02-08T13:56:50.127 回答
1

UITextAttributeTextColor //在 iOS 7 上已弃用。

像这样设置 BarButtonItem 的颜色

    [_barButtonItem setTitleTextAttributes:
                    [NSDictionary dictionaryWithObjectsAndKeys: 
                             [UIColor colorWithRed:250/255.0 
                                             green:240/255.0 
                                             blue:230/255.0 
                                             alpha:1.0],  
                             NSForegroundColorAttributeName,nil] 
                    forState:UIControlStateNormal];
于 2014-04-05T05:21:36.293 回答
1

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

barButtonItem.tintColor = .red
于 2020-10-21T10:24:26.067 回答
0

如果不是您的项目,而您只需要添加一些更改,每个人都应该做的主要事情是检查

[UIBarButtonItem appearance]

我浪费了很多时间才意识到有人设置了错误的 UIBarButtonItem 外观

于 2017-05-21T17:24:58.217 回答