ios 如何更改导航栏上 UIBarButtonItem 的字体颜色/文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12295352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the font color / Text Color of the UIBarButtonItem on navigation bar
提问by user1645721
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.
现在我想以RED Color显示文本“CANCEL”。
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?
怎么做?
采纳答案by IronManGill
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];
回答by IronManGill
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];
回答by GangstaGraham
Just an iOS7 Update with Modern Obj-C Syntax:
只是一个带有现代 Obj-C 语法的 iOS7 更新:
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
回答by miguelghz
UITextAttributeTextColor //Is deprecated on iOS 7.
This code is used for change the text color from from appearance proxy.
此代码用于从外观代理更改文本颜色。
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
回答by Saggy
Here is updated swift 4.0 version code :
这是更新的 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)
回答by Jerry Thomsan
this code is used for change the text color of the UIBarButtonItem on the navigation bar:
此代码用于更改导航栏上 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;
回答by Rool Paap
Old question, here's the swift 2.2 solution:
老问题,这是 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
回答by Abhishek Jain
Swift 4.2
斯威夫特 4.2
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: nil)
doneButton.setTitleTextAttributes([.foregroundColor: UIColor.red], for: .normal)
回答by Shubham Mishra
Swift 4.2
斯威夫特 4.2
UIBarButtonItem using attributed text:
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 using plain text:
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")
}
回答by Oleg Tretiakov
UITextAttributeTextColor //Is deprecated on iOS 7.
UITextAttributeTextColor //在 iOS 7 上已弃用。
Set the color of BarButtonItem in a way like this
像这样设置 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];