【问题标题】:Changing font size of tabbaritem更改 tabbaritem 的字体大小
【发布时间】:2011-02-04 07:37:07
【问题描述】:

是否可以更改标签的字体大小?

【问题讨论】:

    标签: iphone ios cocoa-touch uitabbar uitabbaritem


    【解决方案1】:

    我推荐一个更好的方法:

    [yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor whiteColor], UITextAttributeTextColor, 
        [NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset, 
        [UIFont fontWithName:@"Helvetica" size:18.0], UITextAttributeFont, nil]
        forState:UIControlStateNormal];
    

    【讨论】:

    • 上面这段代码需要在哪个地方写?我在 [self.tabBarController setViewControllers:aControllerList animated:YES]; 之后尝试使用它但这无济于事。
    • 效果很好,我用过这个
    • 对我来说非常棒。从我的应用程序类中添加了 didFinishLaunchingWithOptions 方法。 +1
    【解决方案2】:
    for(UIViewController *tab in  self.tabBarController.viewControllers)
    
    {        
      [tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
      [UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil]
      forState:UIControlStateNormal];
    }
    

    【讨论】:

    • 更新:请参阅 voghDev 对 iOS 7.0+ 版本的回答,以避免弃用 UITextAttributeFont。
    【解决方案3】:

    在 Swift 2.0 中

    override func viewDidLoad() {
        super.viewDidLoad()
    
       let appearance = UITabBarItem.appearance()
       let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.orangeColor()]
       appearance.setTitleTextAttributes(attributes, forState: .Normal)
    
    }
    

    在 Swift 3.0 中

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let appearance = UITabBarItem.appearance()
        let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.orange]
        appearance.setTitleTextAttributes(attributes, for: .normal)
    }
    

    【讨论】:

      【解决方案4】:

      [更新] iOS 7.0+ 版本@cancer86 的好答案:

      [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                         [UIColor whiteColor], NSForegroundColorAttributeName,
                                                         [UIFont fontWithName:@"Helvetica" size:tabFontSize],
                                                         NSFontAttributeName,
                                                         nil] forState:UIControlStateNormal];
      
      [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                         [UIColor redColor], NSForegroundColorAttributeName,
                                                         [UIFont fontWithName:@"Helvetica" size:tabFontSize], NSFontAttributeName,
                                                         nil] forState:UIControlStateSelected];
      

      主要变化是 UITextAttributeTextColor 和 UITextAttributeFont 都被弃用了

      为了将其应用于所有选项卡(感谢@ToolmakerSteve 指出)

      for(UIViewController *tab in  self.tabBarController.viewControllers)
      {        
          [tab.tabBarItem setTitleTextAttributes: ...];
      }
      

      【讨论】:

      • .. 应用于所有选项卡,来自 spatil 的回答 for(UIViewController *tab in self.tabBarController.viewControllers) [tab.tabBarItem setTitleTextAttributes: ...
      • 我们可以为此提供 swift 3 版本
      【解决方案5】:

      在 iOS 5.0 或更高版本中很简单:

      [[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
      

      【讨论】:

      • 很好的解决方案,但使用NSFontAttributeName 而不是UITextAttributeFont
      【解决方案6】:
      TabBarIncreaseFonts(self.tabBarController);
      
      
      void TabBarIncreaseFonts(UITabBarController* customTabBarController)
      {
      
          for(UIView* controlLevelFirst in [customTabBarController.tabBar subviews])
          {
      
              if(![controlLevelFirst isKindOfClass:NSClassFromString(@"UITabBarButton")])
                  continue;
      
              for(id controlLevelSecond in [controlLevelFirst subviews])
              {
                  [controlLevelSecond setBounds: CGRectMake(0, 0, 100, 48)];
      
                  if(![controlLevelSecond isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")])
                       continue;
      
                   [controlLevelSecond setFont: [UIFont boldSystemFontOfSize:20]]; 
                   [controlLevelSecond setFrame: CGRectMake(0, 0, 96, 48)];
                   [controlLevelSecond setTextAlignment:UITextAlignmentCenter];
              }
          }
      }
      

      【讨论】:

        【解决方案7】:

        [将此留在这里供我自己参考,只是对其他工作答案的即兴演奏。对于 mem 来说,它是 iOS 7 的修复程序,这有点超出了问题...]

        @implementation UITabBarController (Util)
        
        - (void) fixForIos7 {
            if (!IS_IOS7)
                return;
            UIFont *tabBarFont = [UIFont systemFontOfSize:12];
            NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                    tabBarFont, UITextAttributeFont, nil];
            for(UIViewController *tab in  self.viewControllers) {
              [tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
            }
        }
        @end
        

        缺少的方法是

        #define IS_IOS7 ( UIDevice.currentDevice.systemVersion.floatValue > 6.9 )
        

        【讨论】:

          【解决方案8】:

          在斯威夫特 4 中

           override func viewDidLoad() {
                  super.viewDidLoad()
                  let appearance = UITabBarItem.appearance()
                  let attributes: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name: "American Typewriter", size: 12)!, NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.orange]
                  appearance.setTitleTextAttributes(attributes, for: .normal)
          
              }
          

          【讨论】:

            【解决方案9】:

            其实是有办法的。

                NSMutableArray *tabBarItems = [[[[[self.view subviews] objectAtIndex:1] subviews] mutableCopy] autorelease];
            
            for (int item = 0; item < [tabBarItems count]; item++) {
                for (int subview = 0; subview < [[[tabBarItems objectAtIndex:item] subviews] count]; subview++) {
                    for (int item = 0; item < [tabBarItems count]; item++)  {
                        for (int subview = 0; subview < [[[tabBarItems objectAtIndex:item] subviews] count]; subview++)  {
                            if ([[[[tabBarItems objectAtIndex:item] subviews] objectAtIndex:subview] isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")]) 
                                [[[[tabBarItems objectAtIndex:item] subviews] objectAtIndex:subview] setFont:[UIFont systemFontOfSize:6.0f]];
                        }
                    }
                }
            }
            

            【讨论】:

              【解决方案10】:

              我认为这在 Swift 中提供了对标签栏颜色和文本属性的清晰控制。

              class MyTabBarController: UITabBarController {
              
                override func viewDidLoad() {
                  super.viewDidLoad()
                  tabBar.barTintColor = UIColor.green
              
                  UITabBarItem.appearance().setTitleTextAttributes(
                      [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 18), 
                         NSAttributedString.Key.foregroundColor: UIColor.orange], for: .normal)
              ...             
              

              【讨论】:

                【解决方案11】:

                Xcode 13 和 Swift 5+

                您可以从自定义扩展类更改标签栏项目字体属性。

                例如,

                1. 创建一个扩展文件 (.swift) 并放置以下代码

                导入 UIKit

                @IBDesignable
                extension UITabBarItem {
                
                @IBInspectable var fontSize : CGFloat{
                    set{
                        if(newValue > 0){
                            let barItemFontAttributes = [NSAttributedString.Key.font : UIFont(name: "Avenir", size: newValue)]
                            self.setTitleTextAttributes(barItemFontAttributes as [NSAttributedString.Key : Any], for: .normal)
                        }
                    }
                    get{
                        return self.fontSize
                    }
                }
                
                1. 现在您可以从属性检查器更改字体大小

                如果有更好的方法请提出!

                【讨论】:

                  猜你喜欢
                  • 2014-10-03
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-01-17
                  • 2013-10-05
                  • 2016-09-04
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多