【问题标题】:Changing selected TabBarItem font iOS更改选定的 TabBarItem 字体 iOS
【发布时间】:2014-10-03 18:33:50
【问题描述】:

我有一个 TabBar。我正在尝试对其进行样式设置,以便 TabBarItems 上的标题对于正常状态和选定状态具有不同的字体。对于正常状态,我想要 Helvetica Neue Light,对于选定状态,我想要 Helvetica Neue Medium。无论我做什么,我似乎都无法让这两个州的字体不同。变色效果很好。这是我目前拥有的:

  // Set the tab bar title appearance for normal state.
  [[UITabBarItem appearance] setTitleTextAttributes:@{
     NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light"
                                          size:16],
     NSForegroundColorAttributeName: [CMK8Colors grayColor]
   }
                                           forState:UIControlStateNormal];

  // Set the tab bar title appearance for selected state.
  [[UITabBarItem appearance] setTitleTextAttributes:@{
     NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Medium"
                                          size:16],
     NSForegroundColorAttributeName: [CMK8Colors blueColor]
   }
                                           forState:UIControlStateSelected];

请帮忙。

【问题讨论】:

    标签: ios uitabbar uitabbaritem


    【解决方案1】:

    好消息和坏消息。

    坏消息,这比只使用外观代理要难一点。

    好消息没那么难!

    标题

    #import <UIKit/UIKit.h>
    
    @interface MYTabbyViewController : UITabBarController
    
    @end
    

    实施

    #import "MYTabbyViewController.h"
    
    @implementation MYTabbyViewController
    
    -(void)setSelectedViewController:(UIViewController *)selectedViewController
    {
        [super setSelectedViewController:selectedViewController];
    
        for (UIViewController *viewController in self.viewControllers) {
            if (viewController == selectedViewController) {
                [viewController.tabBarItem setTitleTextAttributes:@{
                                                        NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Medium" size:16],
                                                        NSForegroundColorAttributeName: [UIColor blueColor]
                                                        }
                                             forState:UIControlStateNormal];
            } else {
                [viewController.tabBarItem setTitleTextAttributes:@{
                                                        NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:16],
                                                        NSForegroundColorAttributeName: [UIColor grayColor]
                                                        }
                                             forState:UIControlStateNormal];
            }
        }
    }
    

    您需要的最后一部分是使用这个子类而不是开箱即用的 UITabBarController。如果您使用 Storyboards,只需选择 TabBarController,转到 Identity Inspector(右侧面板中的第三个子选项卡)并将 UITabBarController 更改为 MYTabBarController 或你有什么。

    但是是的!最重要的是,只需更新 ViewController tabBarItem!

    【讨论】:

    • “子类 UITabBarController 并覆盖 setCurrentViewController 并更新即将选择的 ViewController 的 tabBarItem”听起来很有希望。我是 iOS 新手,代码示例将获得此答案作为所选答案 =)
    • 这有效,但只有在第一次选择之后,当标签栏出现时。由于它不会触发 selectedViewController 它保持正常状态字体。
    • @SoniaCasas 你是说最初选择的 tabBarItem 的外观不正确吗?
    • @Acey 完全正确,因为 selectedViewController 尚未设置。我在 ios 10、11 和 12 中尝试,结果相同。
    【解决方案2】:

    @Acey 在 Swift 3 中的回答:

        override var selectedViewController: UIViewController? {
        didSet {
    
            guard let viewControllers = viewControllers else {
                return
            }
    
            for viewController in viewControllers {
    
                if viewController == selectedViewController {
    
                    let selected: [String: AnyObject] =
                        [NSFontAttributeName: UIFont.systemFont(ofSize: 11, weight: UIFontWeightHeavy),
                         NSForegroundColorAttributeName: UIColor.red]
    
                    viewController.tabBarItem.setTitleTextAttributes(selected, for: .normal)
    
                } else {
    
                    let normal: [String: AnyObject] =
                        [NSFontAttributeName: UIFont.systemFont(ofSize: 11),
                         NSForegroundColorAttributeName: UIColor.blue]
    
                    viewController.tabBarItem.setTitleTextAttributes(normal, for: .normal)
    
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      @Acey's Answer 几乎是完美的,但仅在您第一次选择 tabBarItem 后才更改字体,因此当创建 tabBar 时,将在该时刻为所选的 tabBarItem 创建没有不同的字体。为此,我还在selectedIndex 变量中添加了didSet

      这是完整代码 (Swift 4.2)

      final class TabBarController: UITabBarController {
      
      override var selectedIndex: Int {
          didSet {
              guard let selectedViewController = viewControllers?[selectedIndex] else {
                  return
              }
              selectedViewController.tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 11, weight: UIFontWeightHeavy)], for: .normal)
          }
      }
      
      override var selectedViewController: UIViewController? {
          didSet {
      
              guard let viewControllers = viewControllers else {
                  return
              }
      
              for viewController in viewControllers {
      
                  if viewController == selectedViewController {
      
                      let selected: [NSAttributedString.Key: AnyObject] =
                          [.font: UIFont.systemFont(ofSize: 11, weight: UIFontWeightHeavy)]
      
                      viewController.tabBarItem.setTitleTextAttributes(selected, for: .normal)
      
                  } else {
      
                      let normal: [NSAttributedString.Key: AnyObject] =
                          [.font: UIFont.systemFont(ofSize: 11)]
      
                      viewController.tabBarItem.setTitleTextAttributes(normal, for: .normal)
      
                  }
              }
          }
      }
      

      }

      【讨论】:

        【解决方案4】:

        setSelected 和 setHighlighted 不适用于 UIBarButtonItems。

        使用 UIBarButtonItem 的

        - (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics 
        

        这是文档 - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html

        或者,使用 UIButton。

        UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
        [myButton setTitle:@"Info" forState:UIControlStateNormal];
        [myButton setFont:[UIFont fontWithName:<font name> size:<font size>]];
        
        [myButton setTitleColor:<color>;
        myButton =  <frame>;
        [myButton addTarget:self.webView action:@selector(<action>:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myButton];
        [[UIBarButtonItem appearance] setTintColor:<color>;
        [self.toolbar setItems:[NSArray arrayWithObjects: spaceItem, barButtonItem, nil]];
        

        【讨论】:

        • 我不想更改背景图像。我正在尝试更改字体。在您链接的文档中,它没有提到字体。
        • 正确,但这是赋予选定效果的一种方式。您还可以将带有自定义视图的按钮添加到工具栏。然后您可以轻松更改字体。也使用该选项更新答案。
        • 其实我很抱歉我看错了你的帖子。我没有用标签栏尝试过这个解决方案。看看这篇文章 (appcoda.com/…) - 如果这不起作用请告诉我.. 我会删除我的答案并帮助你解决这个问题
        猜你喜欢
        • 2011-02-04
        • 2011-07-15
        • 1970-01-01
        • 2017-01-24
        • 2016-03-28
        • 1970-01-01
        • 2012-05-12
        • 2012-10-11
        • 1970-01-01
        相关资源
        最近更新 更多