简体   繁体   English

如何更改标签栏项目文本颜色

[英]How to change tab bar item text color

在此处输入图像描述

How can I change the color of "More.." text in tabbar to match with its icon color.如何更改标签栏中“更多..”文本的颜色以匹配其图标颜色。 (Right now Performance is selected in the tab bar) (现在在标签栏中选择了性能)

I tried to set TitleTextAttributes.我试图设置 TitleTextAttributes。

[moreItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName,  [UIColor yellowColor],NSForegroundColorAttributeName , nil]

But it the text color is always set to yellow.但它的文本颜色始终设置为黄色。 even when the item is selected.即使选择了该项目。 Like this像这样在此处输入图像描述

I am trying set to white when selected and when unselected it should match with icon color.我试图在选择时设置为白色,而在未选择时它应该与图标颜色匹配。 Thanks.. Any suggestions will be really helpful.谢谢..任何建议都会非常有帮助。

The accepted answer's code not work for me.接受的答案的代码对我不起作用。

Here is code, that works:这是代码,有效:

    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor yellowColor] }
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
                                             forState:UIControlStateSelected];

I found the answer for my own question.我找到了我自己问题的答案。

We can set perforamceItem setTitleTextAttributes: for two different states.我们可以为两种不同的状态设置perforamceItem setTitleTextAttributes:

  • forState:UIControlStateNormal
  • forState:UIControlStateHighlighted

I added the following code我添加了以下代码

 [performanceItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName,  [UIColor yellowColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];

[performanceItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaLTStd-Roman" size:10.0f], NSFontAttributeName,  [UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateHighlighted];

I need to replace the yellow color with the color of my ICONS.我需要用我的图标的颜色替换黄色。 This is how they are looking now.这就是他们现在的样子。

When More is selected选择更多时

选择更多时

When Performance is Selected选择性能时

选择性能时

Code free way to do this:代码免费的方式来做到这一点:

If you are just using iOS 10 then you may change the Image Tint in your Tab Bar如果您只使用 iOS 10,那么您可以更改标签栏中的图像色调

在此处输入图片说明

If you are also supporting iOS 9 and lower, then you must also add tintColor to your user definer runtime attributes in each tab bar item如果您还支持 iOS 9 及更低版本,那么您还必须将 tintColor 添加到每个选项卡栏项中的用户定义器运行时属性

在此处输入图片说明

if you also wish to change your icon color make sure the correct color image is in your assest folder and change Render as to Original Image如果您还想更改图标颜色,请确保正确的彩色图像位于您的资产文件夹中,并将渲染更改为原始图像

在此处输入图片说明

Swift 5.1 + iOS 12.4 & iOS 13 :斯威夫特 5.1 + iOS 12.4 和 iOS 13

/// Subclass of `UITabBarController` that is used to set certain text colors for tab bar items.
class TabBarController: UITabBarController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let items = tabBar.items {
            // Setting the title text color of all tab bar items:
            for item in items {
                item.setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
                item.setTitleTextAttributes([.foregroundColor: UIColor.lightGray], for: .normal)
            }
        }
    }
}

This is the swift version :-这是快速版本:-

        for item in self.mainTabBar.items! {

          let unselectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
          let selectedItem: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
          item.setTitleTextAttributes(unselectedItem as? [String : AnyObject], forState: .Normal)
          item.setTitleTextAttributes(selectedItem as? [String : AnyObject], forState: .Selected)

        }

Or you can simply change in Appdelegate :-或者您可以简单地更改 Appdelegate :-

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blueColor()], forState: .Selected)
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState: .Normal)
    // Override point for customization after application launch.
    return true
}

Swift 4:斯威夫特 4:

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.red], for: .selected)

For a swift solution, let type inference be your friend:对于快速解决方案,让类型推断成为您的朋友:

override func viewWillAppear(animated: Bool) {
  for item in self.tabBar.items! {
    let unselectedItem = [NSForegroundColorAttributeName: UIColor.blackColor()]
    let selectedItem = [NSForegroundColorAttributeName: UIColor.whiteColor()]

    item.setTitleTextAttributes(unselectedItem, forState: .Normal)
    item.setTitleTextAttributes(selectedItem, forState: .Selected)
  }
}

Swift version of @skywinder answer : @skywinder 的 Swift 版本回答:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected)

This is easy , simply subclass UITabBarItem and assign it to be the class of your tab bar item in either storyboard or code.这很简单,只需将 UITabBarItem 子类化并将其分配为故事板或代码中标签栏项目的类。 The below works perfectly for me.以下非常适合我。

import UIKit

class PPTabBarItem: UITabBarItem {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    override init() {
        super.init()
        commonInit()
    }

    func commonInit() {
        self.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(13), NSForegroundColorAttributeName:UIColor.blackColor()], forState: UIControlState.Normal)

        self.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(13), NSForegroundColorAttributeName:UIColor.yellowColor()], forState: UIControlState.Selected)
    }
}

skywinder's solution is good but it triggers global scope. skywinder 的解决方案很好,但它触发了全局范围。

This works correctly..这工作正常..

 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                        [UIColor redColor], NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor blackColor], NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];

This worked for me on Swift 5.这在 Swift 5 上对我有用。

In AppDelegate :在 AppDelegate 中:

 UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red], for: .selected)

Nowadays if your app supports version of iOS less than 13, you should set this colours different ways:现在,如果您的应用支持低于 13 的 iOS 版本,您应该以不同的方式设置此颜色:

if #available(iOS 13, *) {
    let appearance = UITabBarAppearance()
    appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: .red]
    tabBar.standardAppearance = appearance
} else {
    UITabBarItem.appearance().setTitleTextAttributes(UIColor.red, for: UIControl.State.selected)
}

In code example I set red text color for selected states of UITabBarItem, you may also need to change text color for normal state.在代码示例中,我为 UITabBarItem 的选定状态设置了红色文本颜色,您可能还需要更改正常状态的文本颜色。

You could set the tintcolor and unselectedItemTintColor of UITabBar to change it's appearance.你可以设置UITabBartintcolorunselectedItemTintColor来改变它的外观。

self.tabBar.tintColor = // Your colour
self.tabBar.unselectedItemTintColor = // Your unselected item colour

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM