Changing font size of tabbaritem

34,846

Solution 1

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

Solution 2

for(UIViewController *tab in  self.tabBarController.viewControllers)

{        
  [tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
  [UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil]
  forState:UIControlStateNormal];
}

Solution 3

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

Solution 4

[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: ...];
}

Solution 5

Simple in iOS 5.0 or later:

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
Share:
34,846
4thSpace
Author by

4thSpace

Updated on July 24, 2022

Comments

  • 4thSpace
    4thSpace almost 2 years

    Is it possible to change the font size of tabs?

  • Abhinav
    Abhinav over 11 years
    Which place do you need to write above piece of code? I tries using it after [self.tabBarController setViewControllers:aControllerList animated:YES]; but this does not help.
  • Michael Peterson
    Michael Peterson over 11 years
    Worked great., I used this in
  • voghDev
    voghDev over 9 years
    Worked fantastic for me. Added in didFinishLaunchingWithOptions method from my application class. +1
  • Laszlo
    Laszlo over 7 years
    good solution, but use NSFontAttributeName instead of UITextAttributeFont
  • ToolmakerSteve
    ToolmakerSteve over 7 years
    .. to apply to all tabs, from spatil's answer for(UIViewController *tab in self.tabBarController.viewControllers) [tab.tabBarItem setTitleTextAttributes: ...
  • Ankit Kumar Gupta
    Ankit Kumar Gupta over 7 years
    can we have swift 3 version for this
  • ToolmakerSteve
    ToolmakerSteve over 7 years
    UPDATE: See voghDev's answer for iOS 7.0+ version, that avoids deprecated UITextAttributeFont.