38

Hi I have this code and it doesn't work, what am I doing wrong?

- (void)viewDidLoad
{    
    [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateDisabled];
}

BTW that's not the only thing in my viewDidLoad but I just wanted to show you guys thats where I put it.

jrturton
  • 115,427
  • 31
  • 248
  • 263

6 Answers6

73

As per: How to change the Color of text in UITabBarItem in iOS 5

It looks like the solution may be sending the message to the appearance proxy, instead of one item:

(Deprecated in iOS 7.0+)

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} forState:UIControlStateNormal];

For iOS 7.0+ use:

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} forState:UIControlStateNormal];
pkamb
  • 30,595
  • 22
  • 143
  • 179
Chris Trahey
  • 18,050
  • 1
  • 39
  • 54
17

Swift way, for lazies:

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .selected)
Andrei Konstantinov
  • 6,731
  • 4
  • 39
  • 57
Wujo
  • 1,701
  • 2
  • 22
  • 33
12

Swift 4.1 and custom font

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Montserrat-Medium", size: 11)], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Montserrat-Medium", size: 11)], for: .selected)
Mobile Developer
  • 5,680
  • 1
  • 37
  • 44
4

Swift 3

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "OpenSans", size: 10)!], for: .normal)
Artur
  • 326
  • 2
  • 10
3

Swift 4

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.tabbar], for: .normal)
niggeulimann
  • 648
  • 6
  • 15
0

If I added the code in viewDidLoad() then I was never able to change the font when the tabbar was selected.

This is a great article that explains how to do it with more details: HolySwift Article

In a nutshell, you need to add the following code in your tabbar controller:

override var selectedIndex: Int { 
    didSet {
        guard let selectedViewController = viewControllers?[selectedIndex] else {
            return
        }
        selectedViewController.tabBarItem.setTitleTextAttributes([.font: UIFont.boldSystemFont(ofSize: 13)], for: .normal) 
    }
}

And this:

override var selectedViewController: UIViewController? { 
    didSet {

        guard let viewControllers = viewControllers else {
            return
        }

        for viewController in viewControllers {
            if viewController == selectedViewController {
                viewController.tabBarItem.setTitleTextAttributes([.font: UIFont.boldSystemFont(ofSize: 13)], for: .normal)
            } else {
                viewController.tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 12)], for: .normal)
            }
        }
    }
}

PS: This will work with custom fonts as well.

christostsang
  • 1,447
  • 3
  • 20
  • 41