如何更改导航栏上UIBarButtonItem的字体颜色/文本颜色

33

我通过以下方式以编程方式将一个导航栏按钮添加到导航栏:

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

现在我想展示“CANCEL”文本为红色

我的意思是,我需要更改栏按钮项上的文本,而不是按钮的色调颜色。

如何实现?


请查看以下链接:https://dev59.com/QXA75IYBdhLWcg3wZ4Nr 和 https://dev59.com/N3RB5IYBdhLWcg3wUFrB - IronManGill
对于iOS 5+,请参考以下答案:https://dev59.com/4Gsz5IYBdhLWcg3wj4ra - Chris Hinkle
12个回答

107

看这个:

  UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:nil action:nil];
[cancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor],  UITextAttributeTextColor,nil] forState:UIControlStateNormal];

它导致崩溃:线程1:EXC_BAD_ACESS(code=2 address=0x0) - user1645721
1
+1,它完美地工作了。谢谢。@ user1645721 setTitleTextAttributes函数从IOS 5可用。 - Asta ni enohpi
6
需要说明的是,UITextAttributeTextColor在7.0中被替换为NSForegroundColor - Mark Adams
3
NSForegroundColorAttributeName是UITextAttributeTextColor的正确替换。 - Gene Z. Ragan

33

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

[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];

13
UITextAttributeTextColor //Is deprecated on iOS 7. 

这段代码用于通过外观代理更改文本颜色。

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

1
这个设置背景颜色,他要求文本颜色。 - Lukasz
请使用NSForegroundColorAttributeName代替UITextAttributeTextColor。 - Bogdan

9

以下是更新的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)

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

4

Swift 4.2

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

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;

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

3

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

2
如果您需要设置与默认外观不同的颜色,请使用:
barButtonItem.tintColor = .red

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接