39

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
  • 118,105
  • 32
  • 252
  • 268

7 Answers7

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
  • 33,281
  • 23
  • 160
  • 191
Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
18

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,971
  • 4
  • 41
  • 57
Wujo
  • 1,845
  • 2
  • 25
  • 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,730
  • 1
  • 39
  • 45
5

Swift 3

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

Swift 4

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

Swift 5

Lets say you have a class that inherits UITabBarController like below:

final class YourTabBarController: UITabBarController { .. }

Define 2 types of attribute in this YourTabBarController:

let normalTabBarAttributes: [NSAttributedString.Key: Any] = [
    .font: NotSelectedFont,
    .foregroundColor: NotSelectedColor
]
let selectedTabBarAttributes: [NSAttributedString.Key: Any] = [
    .font: SelectedFont,
    .foregroundColor: SelectedColor
]

And you have a ViewController named YourViewController that contains the TabBar and inherits UIViewController like below:

final class YourViewController: UIViewController {...}

iOS 15.0 or later version

Define a method in YourViewController:

private func setTabBarAttributes() {
    guard let yourTabBarController = tabBarController as? YourTabBarController else {
        return
    }
    let appearance = UITabBarAppearance()
    appearance.stackedLayoutAppearance.normal.titleTextAttributes = yourTabBarController.normalTabBarAttributes
    appearance.stackedLayoutAppearance.selected.titleTextAttributes = yourTabBarController.selectedTabBarAttributes
    yourTabBarController.tabBar.standardAppearance = appearance
    yourTabBarController.tabBar.scrollEdgeAppearance = appearance
}

Form override func viewDidLoad() method of YourViewController call that method:

setTabBarAttributes()

previous versions of iOS 15.0

In YourTabBarController you can define a method :

private func updateTabBarAttributes() {
    guard let viewControllers = viewControllers else { return }
    for viewController in viewControllers {
        if viewController == selectedViewController {
            viewController.tabBarItem.setTitleTextAttributes(selectedTabBarAttributes, for: .normal)
        } else {
            viewController.tabBarItem.setTitleTextAttributes(normalTabBarAttributes, for: .normal)
        }
    }
}

Now use the method like below:

override var selectedIndex: Int {
    didSet {
        updateTabBarAttributes()
    }
}

override var selectedViewController: UIViewController? {
    didSet {
        updateTabBarAttributes()
    }
}
SM Abu Taher Asif
  • 2,221
  • 1
  • 12
  • 14
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,701
  • 3
  • 27
  • 46