user2636197
user2636197

Reputation: 4122

swift change tab bar title font

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

Answers (7)

Vladimir
Vladimir

Reputation: 21

guard let font: UIFont = UIFont(font: "arial", size: 15) else { return }
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: font], for: .normal)

Upvotes: 2

9ezi
9ezi

Reputation: 1

Swift 4.2 Worked for me

Add this code to mainTabView

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: font], for: .normal)

Upvotes: 0

ArturRuz
ArturRuz

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

zedaexa
zedaexa

Reputation: 17

I found this Swift 5 solution to be useful:

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

Upvotes: 0

slessans
slessans

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

Ahmadreza
Ahmadreza

Reputation: 7212

Swift 4.1

UITabBarItem.appearance().setTitleTextAttributes([kCTFontAttributeName as NSAttributedStringKey: font], for: .normal)

Upvotes: 0

flame3
flame3

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

Related Questions