【发布时间】:2011-03-19 20:19:46
【问题描述】:
我想把颜色改成红色。
【问题讨论】:
标签: iphone objective-c cocoa-touch interface-builder uibarbuttonitem
我想把颜色改成红色。
【问题讨论】:
标签: iphone objective-c cocoa-touch interface-builder uibarbuttonitem
我找到了一种更简单的方法来更改标题的颜色: 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];
【讨论】:
此问题已回答here。基本上,您必须创建一个 UIButton,根据需要对其进行配置,然后使用 UIButton 将 Bar Button Item 初始化为自定义视图。
【讨论】:
有两种方法可以改变 UIBarButtonITem 显示的 Text 的颜色。
1)
[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];
添加以下代码行您要更改颜色的视图控制器
【讨论】: