Changing font size of tabbaritem

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

Is it possible to change the font size of tabs?

相关标签:
10条回答
  • 2020-11-30 06:53
    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];
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 06:56

    Simple in iOS 5.0 or later:

    [[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
    
    0 讨论(0)
  • 2020-11-30 06:57

    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)
    ...             
    
    0 讨论(0)
  • 2020-11-30 07:02

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

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

    [Update] iOS 7.0+ version of @cancer86's nice answer:

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

    The main change is that UITextAttributeTextColor and UITextAttributeFont are both deprecated

    In order to apply it to all tabs (thanks to @ToolmakerSteve for pointing out)

    for(UIViewController *tab in  self.tabBarController.viewControllers)
    {        
        [tab.tabBarItem setTitleTextAttributes: ...];
    }
    
    0 讨论(0)
  • 2020-11-30 07:04

    Actually there is a way.

        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]];
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题