Changing tab bar font in Swift

后端 未结 7 1103
南方客
南方客 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:39

    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)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-16 10:49

    Put this under didFinishLaunchingWithOptions:

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

    This works in Swift 4

    0 讨论(0)
  • 2020-12-16 10:53

    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)
    
    0 讨论(0)
  • 2020-12-16 10:54

    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)
    
    0 讨论(0)
  • 2020-12-16 11:01

    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)
    
    0 讨论(0)
提交回复
热议问题