我按照以下步骤向导航栏添加了一个条形按钮
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"CANCEL" style:UIBarButtonItemStyleBordered target:self action:@selector(goToPreviousView)];
self.navigationItem.leftBarButtonItem = cancel;
现在我要在RED Color中显示文本“CANCEL”。
我的意思是我需要更改条形按钮项上的文字,而不是按钮的色调。
怎么做?
答案 0 :(得分:104)
检查出来: -
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:nil action:nil];
[cancel setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
答案 1 :(得分:32)
使用现代Obj-C语法的iOS7更新:
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
答案 2 :(得分:12)
UITextAttributeTextColor //Is deprecated on iOS 7.
此代码用于更改外观代理的文本颜色。
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
答案 3 :(得分: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 :(得分:5)
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)
答案 5 :(得分: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;
答案 6 :(得分: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
答案 7 :(得分:0)
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];
答案 8 :(得分:0)
如果不是你的项目而你只需要添加一些更改,那么每个人都应该做的主要事情是检查
[UIBarButtonItem appearance]
我浪费了很多时间才意识到有人设置错误的UIBarButtonItem外观
答案 9 :(得分:0)
Swift 4.2
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: nil)
doneButton.setTitleTextAttributes([.foregroundColor: UIColor.red], for: .normal)
答案 10 :(得分:0)
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")
}
答案 11 :(得分:0)
如果您需要设置与默认外观不同的颜色,请使用:
barButtonItem.tintColor = .red