更改Swift中的标签栏字体

时间:2014-09-26 22:43:42

标签: swift uitabbar uifont

我一直在尝试更改标签栏项目的字体,但我还没有找到任何Swift示例。我知道这就是你在Objective-C中改变它的方式:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateNormal];

但是我怎么能把它翻译成Swift?

7 个答案:

答案 0 :(得分:48)

UITextAttributeFont在iOS 7中已弃用。您应该使用NS变体:

import UIKit

let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes, forState: .Normal)

答案 1 :(得分:30)

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)

注意:对setTitleTextAttributes.normal使用.selected,让更改保持选择状态更改。

答案 2 :(得分:7)

将其放在didFinishLaunchingWithOptions

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

这适用于Swift 4

答案 3 :(得分:4)

另外@ Mc.Lover的答案,如果您想对应用程序中的所有标签栏项目进行此更改,我建议在AppDelegate类的application函数中添加代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    //Just add this line to get it done.       
    UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)

    return true
}

答案 4 :(得分:2)

在Swift4版本中,您可以使用属性键设置字体和前景色

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)

答案 5 :(得分:2)

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)

答案 6 :(得分:0)

快速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)