david
david

Reputation: 2958

How do I change only the font size in UITabBar Item?

How do I change only the font size in UITabBar Item?

In my AppDelegate:

I added this

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // [START initialize_firebase]
        FirebaseApp.configure()
        // [END initialize_firebase]
        // Override point for customization after application launch.


    let selectedAttr = UITabBarItem.appearance().titleTextAttributes(for: UIControlState.selected)
    let normalAttr = UITabBarItem.appearance().titleTextAttributes(for: UIControlState.normal)
    //
    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: normalAttr.fontName, size: 15)!], for: UIControlState.normal)


    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: normalAttr.fontname, size: 15)!], for: UIControlState.selected)

        return true
    }

But I don't see any normalAttr.fontName property. How do I fix this?

Upvotes: 0

Views: 146

Answers (1)

Oliver Atkinson
Oliver Atkinson

Reputation: 8029

titleTextAttributes(for:) returns a dictionary which you can use to key to find the font.

    let normalAttr = UITabBarItem.appearance().titleTextAttributes(for: UIControlState.normal)
    let font = normalAttr[NSFontAttributeName] as! UIFont // <-- This instance has a property called `fontName`
...
    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: font.fontName, size: 15)!], for: UIControlState.normal)

Upvotes: 1

Related Questions