user3746428
user3746428

Reputation: 11175

Changing tab bar font in Swift

I have been trying to change the font for the tab bar items however I haven't been able to find any Swift examples. I know that this is how you change it in Objective-C:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateNormal];

But how can I translate this into Swift?

Upvotes: 41

Views: 51314

Answers (9)

Mantas Laurinavičius
Mantas Laurinavičius

Reputation: 951

Call updateFonts function on didSelect and init:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    updateFonts()
}

func updateFonts() {
    tabBar.items?.forEach({ (item) in
        item.setTitleTextAttributes([NSAttributedString.Key.font: Theme.shared.regularFont(12)], for: .normal)
    })

    tabBar.selectedItem?.setTitleTextAttributes([NSAttributedString.Key.font: Theme.shared.semiboldFont(12)], for: .normal)
}

Upvotes: 0

McDonal_11
McDonal_11

Reputation: 4075

One more Answer, but for iOS 15:

if #available(iOS 15, *) {
    let tabBarAppearance = UITabBarAppearance()
    tabBarAppearance.backgroundColor = backgroundColor
    tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selectedItemTextColor, .font: UIFont(name: "American Typewriter", size: 20)]
    tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unselectedItemTextColor, .font: UIFont(name: "American Typewriter", size: 20)]
    tabBar.standardAppearance = tabBarAppearance
    tabBar.scrollEdgeAppearance = tabBarAppearance
}

Upvotes: 6

Dani
Dani

Reputation: 1288

Swift 5

let appearance = UITabBarItem.appearance()
let attributes = [NSAttributedString.Key.font:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes as [NSAttributedString.Key : Any], for: .normal)

Upvotes: 8

iOS.Lover
iOS.Lover

Reputation: 6051

Swift 4.2

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

Swift 4

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

Swift 3

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .selected)

Note: Use setTitleTextAttributes for both .normal and .selected to have changes persist selection state changes.

Upvotes: 63

Ahmed Safadi
Ahmed Safadi

Reputation: 4590

Swift 4.2

    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "BahijTheSansArabic-Plain", size: 10)!], for: .normal)
    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "BahijTheSansArabic-Plain", size: 10)!], for: .selected)

Upvotes: 3

Madhav bhogapurapu
Madhav bhogapurapu

Reputation: 21

In Swift4 version, you can use attribute keys to set font and foreground color

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor(hexString: "#D8FFE8"), NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Bold", size: 16) as Any], for: .normal)
    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor(hexString: "#FFFFFF"),NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Bold", size: 16) as Any], for: .selected)

Upvotes: 2

Milad Faridnia
Milad Faridnia

Reputation: 9477

In addition @Mc.Lover 's answer, If you want to aplly this change to all of your tab bar Items in application, I recommand to add the code in application function of AppDelegate class:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    //Just add this line to get it done.       
    UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)

    return true
}

Upvotes: 4

Richard
Richard

Reputation: 111

Put this under didFinishLaunchingWithOptions:

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

This works in Swift 4

Upvotes: 11

AlBlue
AlBlue

Reputation: 24040

The UITextAttributeFont was deprecated in iOS 7. You should use the NS variant instead:

import UIKit

let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes, forState: .Normal)

Upvotes: 68

Related Questions