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)
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 01, 2021Comments
-
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 about 12 yearschange forstate:UIControlStateNormal
-
sam_smith over 9 yearsNot explicitly mentioned here. You can put this code in the didFinishLaunchingWithOptions function in the app delegate to set it for the app
-
Reinhold about 9 yearsThat worked for me in swift: UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "AmericanTypewriter", size: 20.0)] as [NSObject : AnyObject!], forState: UIControlState.Normal)
-
DawnSong almost 9 yearsNSFontAttributeName for iOS7+, and UITextAttributeFont for iOS6-
-
Iulian Onofrei over 8 years
@{NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]}
for 2015. -
AydinAngouti over 7 yearsThis doesn't change the font for me for
.selected
state. -
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 almost 6 yearschange different font for Selected state is no effect
-
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 almost 2 yearsUpdate, as of iOS 15, if the UITabBar uses UITabBarItemAppearance(), it will override any fonts set in the UITabBarItem.appearance() setting