简体   繁体   English

如何仅更改UITabBar项目中的字体大小?

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

How do I change only the font size in UITabBar Item? 如何仅更改UITabBar项目中的字体大小?

In my AppDelegate: 在我的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. 但是我没有看到任何normalAttr.fontName属性。 How do I fix this? 我该如何解决?

titleTextAttributes(for:) returns a dictionary which you can use to key to find the font. titleTextAttributes(for:)返回一个字典,您可以使用该字典来查找字体。

    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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM