Reputation: 4122
I wonder how I can change the font and size of title in my tabs when I use tab bar.
I have looked in the docs and I cant find anything about title font and size - source
Upvotes: 3
Views: 12162
Reputation: 21
guard let font: UIFont = UIFont(font: "arial", size: 15) else { return }
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)
Upvotes: 2
Reputation: 1
Swift 4.2 Worked for me
Add this code to mainTabView
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: font], for: .normal)
Upvotes: 0
Reputation: 59
In my case this solution worked for me (Swift 5.5):
let fontSize: CGFloat = 12
if #available(iOS 13, *) {
let appearance = tabBarController.tabBar.standardAppearance
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
]
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
]
} else {
if #available(iOS 11, *) {
UITabBarItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
], for: .selected)
}
}
Upvotes: 0
Reputation: 17
I found this Swift 5 solution to be useful:
UITabBarItem.appearance().setTitleTextAttributes([.font: UIFont(name: "FontName", size: 10)!], for: .normal)
Upvotes: 0
Reputation: 687
You can change it via the appearance proxy:
let font: UIFont = ...
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: font], forState: .Normal)
Swift 4:
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: font], for: .normal)
You should put this in your app delegate in func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
Upvotes: 18
Reputation: 7212
Swift 4.1
UITabBarItem.appearance().setTitleTextAttributes([kCTFontAttributeName as NSAttributedStringKey: font], for: .normal)
Upvotes: 0
Reputation: 2992
Update for swift 3.
Put this in your app delegate in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: yourFont], for: .normal)
Upvotes: 2