我想把颜色改为红色。
我想把颜色改为红色。
我找到了一种更简单的方法来改变标题的颜色: iOS7:
UIBarButtonItem *button =
[[UIBarButtonItem alloc] initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[button setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], NSForegroundColorAttributeName,nil]
forState:UIControlStateNormal];
在 iOS7 之前:
UIBarButtonItem *button =
[[UIBarButtonItem alloc] initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[button setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], UITextAttributeTextColor,nil]
forState:UIControlStateNormal];
[barButtonITem setTintColor:[UIColor redColor]];
2) 或者你可以使用任何UIColor类方法。这是一种非常有效的解决方案。尽管对于iOS 7,您可以使用NSForegroundColorAttributeName。
barButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
在 Guvvy Ava 的类似提示下,您可以像这样更改字体大小:
[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:26.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
要求使用 Swift 版本 4.0 或更高版本
barButtonITem.tintColor = UIColor.red // for textColor change
barButtonITem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor :UIColor.red,NSAttributedStringKey.font:UIFont.init(name:"Avenir", size:15.0)], for: UIControlState.normal)
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
要改变按钮文本颜色,也可以使用以下代码。
[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];