Changing tab bar font in Swift

后端 未结 7 1106
南方客
南方客 2020-12-16 10:03

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:

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 10:40

    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.

提交回复
热议问题