36
votes

Is it possible to change the font size of tabs?

11

11 Answers

61
votes

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];
20
votes
for(UIViewController *tab in  self.tabBarController.viewControllers)

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

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

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

Simple in iOS 5.0 or later:

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
5
votes
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];
        }
    }
}
1
votes

[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 )
1
votes

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
votes

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
votes

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
votes

Xcode 13 and Swift 5+

you can change tab bar item font attributes from the custom extension class.

for example,

  1. create an extension file (.swift) and place the following code

import 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. now you can change the font size from attribute inspector

enter image description here

if there is a better way please mention it!