【问题标题】:Changing tab bar font in Swift在 Swift 中更改标签栏字体
【发布时间】:2014-11-22 01:00:11
【问题描述】:

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

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

但是我怎样才能把它翻译成 Swift 呢?

【问题讨论】:

    标签: swift uitabbar uifont


    【解决方案1】:

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

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

    【讨论】:

    • 没关系,我将代码放入 awakeFromNib 并修复了它。非常感谢!
    • 注意:使用 Playground 是发现这类事情的好方法。您还可以查看swifter.natecook.com 以了解其中一些名称。
    • 从 XCode 6.1 开始,这似乎不起作用。我得到的“NSString”与“NSObject”不同。你们也有这种情况吗?
    • 从 6.1 开始,您必须解开字体。放!在你的字体初始化之后
    • 这不会为我更改 .selected 状态的字体。
    【解决方案2】:

    斯威夫特 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)
    

    斯威夫特 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)
    

    斯威夫特 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 以使更改保持选择状态更改。

    【讨论】:

    • 这不会为我更改 .selected 状态的字体。
    • @AbbasAngouti - 您只需复制代码并将.normal 更改为.selected
    • 我应该把这段o代码放在哪里?我应该为 TabBarController 创建一个类吗?
    • 可以确认它不适用于所选
    【解决方案3】:

    除了@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】:

      把这个放在didFinishLaunchingWithOptions下面:

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

      这适用于 Swift 4

      【讨论】:

      • 对我来说,在 UITabBarItem.appearance() 上调用此方法不起作用。必须用 self.navigationItem.rightBarButtonItem 替换它?
      【解决方案5】:

      在 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)
      

      【讨论】:

      • 不适用于所选
      【解决方案6】:

      斯威夫特 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)
      

      【讨论】:

      • @bibscy in AppDelegate.swift inside func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { }
      【解决方案7】:

      斯威夫特 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)
      

      【讨论】:

        猜你喜欢
        • 2017-01-19
        • 2016-08-12
        • 2014-12-23
        • 2018-04-30
        • 1970-01-01
        • 1970-01-01
        • 2014-12-23
        • 2014-10-12
        • 1970-01-01
        相关资源
        最近更新 更多