Changing font in UITabBarItem

24,585

Solution 1

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

Solution 2

Swift way, for lazies:

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .selected)

Solution 3

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)

Solution 4

Swift 3

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

Solution 5

Swift 4

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.tabbar], for: .normal)
Share:
24,585
Admin
Author by

Admin

Updated on July 01, 2021

Comments

  • Admin
    Admin almost 3 years

    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.

  • Paresh Navadiya
    Paresh Navadiya about 12 years
    change forstate:UIControlStateNormal
  • sam_smith
    sam_smith over 9 years
    Not explicitly mentioned here. You can put this code in the didFinishLaunchingWithOptions function in the app delegate to set it for the app
  • Reinhold
    Reinhold about 9 years
    That worked for me in swift: UITabBarItem.appearance().setTitleTextAttributes([NSFontAttr‌​ibuteName: UIFont(name: "AmericanTypewriter", size: 20.0)] as [NSObject : AnyObject!], forState: UIControlState.Normal)
  • DawnSong
    DawnSong almost 9 years
    NSFontAttributeName for iOS7+, and UITextAttributeFont for iOS6-
  • Iulian Onofrei
    Iulian Onofrei over 8 years
    @{NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} for 2015.
  • AydinAngouti
    AydinAngouti over 7 years
    This doesn't change the font for me for .selected state.
  • Timur Bernikovich
    Timur Bernikovich over 6 years
    @AbbasAngouti for changing font is selected state you should use something like this: stackoverflow.com/a/47529912/1980246
  • Beyond Chao
    Beyond Chao almost 6 years
    change different font for Selected state is no effect
  • Chris Trahey
    Chris Trahey almost 6 years
    @BeyondChao it might be worth asking a fresh question (or searching differently) as this answer is now over 6 years old! Good luck!
  • PhoenixB
    PhoenixB almost 2 years
    Update, as of iOS 15, if the UITabBar uses UITabBarItemAppearance(), it will override any fonts set in the UITabBarItem.appearance() setting