Changing font size of tabbaritem
34,846
Solution 1
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];
Solution 2
for(UIViewController *tab in self.tabBarController.viewControllers)
{
[tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil]
forState:UIControlStateNormal];
}
Solution 3
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)
}
Solution 4
[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: ...];
}
Solution 5
Simple in iOS 5.0 or later:
[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
Author by
4thSpace
Updated on July 24, 2022Comments
-
4thSpace almost 2 years
Is it possible to change the font size of tabs?
-
Abhinav over 11 yearsWhich place do you need to write above piece of code? I tries using it after [self.tabBarController setViewControllers:aControllerList animated:YES]; but this does not help.
-
Michael Peterson over 11 yearsWorked great., I used this in
-
voghDev over 9 yearsWorked fantastic for me. Added in didFinishLaunchingWithOptions method from my application class. +1
-
Laszlo over 7 yearsgood solution, but use
NSFontAttributeName
instead ofUITextAttributeFont
-
ToolmakerSteve over 7 years.. to apply to all tabs, from spatil's answer
for(UIViewController *tab in self.tabBarController.viewControllers) [tab.tabBarItem setTitleTextAttributes: ...
-
Ankit Kumar Gupta over 7 yearscan we have swift 3 version for this
-
ToolmakerSteve over 7 yearsUPDATE: See voghDev's answer for iOS 7.0+ version, that avoids deprecated UITextAttributeFont.