繁体   English   中英

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

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

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

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

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

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

怎么做?

看一下这个 :-

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

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

另一种方法是:-

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

此代码用于更改导航栏上 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;

老问题,这是 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

斯威夫特 4.2

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

斯威夫特 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")
    }

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

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

barButtonItem.tintColor = .red

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

[UIBarButtonItem appearance]

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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