Changing font size of tabbaritem

后端 未结 10 1690
忘掉有多难
忘掉有多难 2020-11-30 06:18

Is it possible to change the font size of tabs?

相关标签:
10条回答
  • 2020-11-30 07:08
    for(UIViewController *tab in  self.tabBarController.viewControllers)
    
    {        
      [tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
      [UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil]
      forState:UIControlStateNormal];
    }
    
    0 讨论(0)
  • 2020-11-30 07:08

    IN Swift 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)
    
        }
    
    0 讨论(0)
  • 2020-11-30 07:09

    [leaving this here for my own reference, just a riff off the other working answers. For mem it's a fix for iOS 7, which is beyond the question by a bit...]

    @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
    

    the missing method is

    #define IS_IOS7 ( UIDevice.currentDevice.systemVersion.floatValue > 6.9 )
    
    0 讨论(0)
  • 2020-11-30 07:15

    I recommend a better way:

    [yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor whiteColor], UITextAttributeTextColor, 
        [NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset, 
        [UIFont fontWithName:@"Helvetica" size:18.0], UITextAttributeFont, nil]
        forState:UIControlStateNormal];
    
    0 讨论(0)
提交回复
热议问题