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

时间:2018-03-11 08:48:13

标签: ios swift

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

在我的AppDelegate中:

我添加了这个

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
    }

但我没有看到任何normalAttr.fontName属性。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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)