Changing font size of tabbaritem

2020-01-27 04:15发布

Is it possible to change the font size of tabs?

10条回答
迷人小祖宗
2楼-- · 2020-01-27 04:33

Simple in iOS 5.0 or later:

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
查看更多
Lonely孤独者°
3楼-- · 2020-01-27 04:34
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];
        }
    }
}
查看更多
不美不萌又怎样
4楼-- · 2020-01-27 04:34

I think this in Swift gives a clear control over the tab bar colors and text attributes.

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)
...             
查看更多
一夜七次
5楼-- · 2020-01-27 04:38

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)

    }
查看更多
Ridiculous、
6楼-- · 2020-01-27 04:46

[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 )
查看更多
在下西门庆
7楼-- · 2020-01-27 04:50
for(UIViewController *tab in  self.tabBarController.viewControllers)

{        
  [tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
  [UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil]
  forState:UIControlStateNormal];
}
查看更多
登录 后发表回答