【问题标题】:How to change tab bar item text color如何更改标签栏项目文本颜色
【发布时间】:2013-09-12 08:22:52
【问题描述】:

如何更改标签栏中“更多..”文本的颜色以匹配其图标颜色。 (现在在标签栏中选择了性能)

我尝试设置 TitleTextAttributes。

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

但是文本颜色总是设置为黄色。即使选择了该项目。像这样

我尝试在选中时设置为白色,在未选中时它应该与图标颜色匹配。 谢谢.. 任何建议都会很有帮助。

【问题讨论】:

  • 不改文字,为什么不能用图片?看看herehere
  • 我找到了一种更好的方法来更改它,即使用图像作为我的文本。请看我的回答。如果我做错了,请告诉我。
  • 好吧,如果您满意,请继续。

标签: iphone ios objective-c ios7


【解决方案1】:

现在,如果您的应用支持低于 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)
}

在代码示例中,我为 UITabBarItem 的选定状态设置了红色文本颜色,您可能还需要更改正常状态的文本颜色。

【讨论】:

    【解决方案2】:

    Swift 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)
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      这在 Swift 5 上对我有用。

      在 AppDelegate 中:

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

      【讨论】:

        【解决方案4】:

        斯威夫特 4:

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

        【讨论】:

          【解决方案5】:

          这可以正常工作..

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

          【讨论】:

            【解决方案6】:

            无需代码的方式:

            如果您只是使用 iOS 10,那么您可以更改标签栏中的图像色调

            如果您还支持 iOS 9 及更低版本,那么您还必须将 tintColor 添加到每个选项卡栏项中的用户定义器运行时属性

            如果您还想更改图标颜色,请确保您的资产文件夹中包含正确的颜色图像并将渲染更改为原始图像

            【讨论】:

            • 漂亮的答案。
            • 谢谢@Adeel :)
            • 我认为每一种形式的美都应该被欣赏。
            【解决方案7】:

            @skywinder 答案的 Swift 版本:

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

            【讨论】:

              【解决方案8】:

              这是快速版本:-

                      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)
              
                      }
              

              或者您可以简单地更改 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
              }
              

              【讨论】:

                【解决方案9】:

                这很简单,只需子类化 UITabBarItem 并将其指定为情节提要或代码中标签栏项目的类。以下内容对我来说非常适合。

                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 的解决方案不错,但是会触发全局作用域。

                【讨论】:

                  【解决方案10】:

                  要快速解决问题,请让类型推断成为您的朋友:

                  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)
                    }
                  }
                  

                  【讨论】:

                    【解决方案11】:

                    接受的答案代码对我不起作用。

                    这是有效的代码:

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

                    【讨论】:

                      【解决方案12】:

                      我找到了自己问题的答案。

                      我们可以为两种不同的状态设置perforamceItem setTitleTextAttributes:

                      • forState:UIControlStateNormal
                      • forState:UIControlStateHighlighted

                      我添加了以下代码

                       [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];
                      

                      我需要用我的图标颜色替换黄色。这就是他们现在的样子。

                      选择更多时

                      选择性能时

                      【讨论】:

                      猜你喜欢
                      • 2013-11-17
                      • 2015-09-15
                      • 1970-01-01
                      • 2018-03-09
                      • 2016-03-29
                      • 2022-08-22
                      • 1970-01-01
                      • 2014-07-15
                      • 1970-01-01
                      相关资源
                      最近更新 更多