如何更改 UIBarButton 项目的字体颜色?

发布于 2024-09-11 09:05:23 字数 17 浏览 1 评论 0原文

我想把颜色改成红色。

I want to change the color to red.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

攒一口袋星星 2024-09-18 09:05:23

我找到了一种更简单的方法来更改标题的颜色:
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];

I found a simpler way to just change the color of title:
iOS7:

UIBarButtonItem *button = 
 [[UIBarButtonItem alloc] initWithTitle:@"Title" 
                                  style:UIBarButtonItemStyleBordered 
                                 target:nil 
                                 action:nil];
[button setTitleTextAttributes:
          [NSDictionary dictionaryWithObjectsAndKeys: 
               [UIColor redColor], NSForegroundColorAttributeName,nil] 
                                            forState:UIControlStateNormal];

and prior iOS7:

UIBarButtonItem *button = 
  [[UIBarButtonItem alloc] initWithTitle:@"Title" 
                                   style:UIBarButtonItemStyleBordered 
                                  target:nil 
                                  action:nil];
[button setTitleTextAttributes:
          [NSDictionary dictionaryWithObjectsAndKeys: 
               [UIColor redColor], UITextAttributeTextColor,nil] 
                                    forState:UIControlStateNormal];
听不够的曲调 2024-09-18 09:05:23

此处回答了这个问题。基本上,您必须创建一个 UIButton,根据需要配置它,然后使用 UIButton 作为自定义视图初始化 Bar Button Item。

This question was answered here. Basically, you have to create a UIButton, configure it as you wish, and then initialize the Bar Button Item with the UIButton as a custom view.

青春有你 2024-09-18 09:05:23

有两种方法可以更改 UIBarButtonITem 显示的文本的颜色。

1)

[barButtonITem setTintColor:[UIColor redColor]];

2) 或者您可以使用任何 UIColor 类方法。这是非常有效的解决方案。尽管对于 iOS 7,您可以使用 NSForegroundColorAttributeName 并可以使用

barButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];

There are Two ways to change the color of the Text displayed UIBarButtonITem.

1)

[barButtonITem setTintColor:[UIColor redColor]];

2) or you can you any UIColor class method. this is really efficient solution.Althought for iOS 7 you can use NSForegroundColorAttributeName and can use

barButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
怀里藏娇 2024-09-18 09:05:23

根据 Guvvy Ava 的类似说明,您可以更改字体大小,例如

[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:26.0], UITextAttributeFont,nil] forState:UIControlStateNormal];

On similar note from Guvvy Ava you can change font size like

[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Helvetica-Bold" size:26.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
也只是曾经 2024-09-18 09:05:23

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)

swift version 4.0 or later

barButtonITem.tintColor = UIColor.red   // for textColor change

if want to change font as well as text color use the following code

barButtonITem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor :UIColor.red,NSAttributedStringKey.font:UIFont.init(name:"Avenir", size:15.0)], for: UIControlState.normal)
恋你朝朝暮暮 2024-09-18 09:05:23

如果您使用系统导航栏按钮项目,则要更改按钮色调颜色,请使用

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

要更改按钮文本颜色,也请使用以下代码。

[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];

添加以下代码行来更改您想要更改颜色的视图控制器

If you are using system Navigation bar button item then to change your button tint color use

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

To change button text color use the following code also.

[[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];

Add the following line of code which viewcontroller you want to change your color

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文