【问题标题】:swift change tab bar title font快速更改标签栏标题字体
【发布时间】:2016-08-12 05:39:50
【问题描述】:

我想知道如何在使用标签栏时更改标签中标题的字体和大小。

我查看了文档,但找不到任何关于标题字体和大小的信息 - source

【问题讨论】:

    标签: ios swift fonts uitabbarcontroller


    【解决方案1】:

    您可以通过外观代理更改它:

    let font: UIFont = ...
    UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: font], forState: .Normal)
    

    斯威夫特 4:

    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: font], for: .normal)
    

    你应该把它放在你的应用代理中func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

    【讨论】:

    • @user2636197 查看更新后的答案,如果这对您有用,请接受
    • 我收到错误:“UITabBarItem”类型的值没有成员“titleTextAttributes”
    • @user2636197 我的错,检查新的编辑。您必须指定状态。 .Normal 状态将其设置为正常,其他状态将回退到此值。
    • 谢谢,错误消失了,但我的字体没有改变
    【解决方案2】:

    Swift 3 的更新。

    把它放在你的应用代理中func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool

    UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: yourFont], for: .normal)
    

    【讨论】:

      【解决方案3】:

      斯威夫特 5.5

      let font: UIFont = UIFont(font: "arial", size: 15)! UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: 字体],用于:.normal)

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案4】:

      斯威夫特 4.1

      UITabBarItem.appearance().setTitleTextAttributes([kCTFontAttributeName as NSAttributedStringKey: font], for: .normal)
      

      【讨论】:

        【解决方案5】:

        我发现这个 Swift 5 解决方案很有用:

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

        【讨论】:

          【解决方案6】:

          在我的情况下,这个解决方案对我有用(Swift 5.5):

          let fontSize: CGFloat = 12
                
          if #available(iOS 13, *) {
              let appearance = tabBarController.tabBar.standardAppearance
              appearance.stackedLayoutAppearance.normal.titleTextAttributes = [
                  NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
              ]
              appearance.stackedLayoutAppearance.selected.titleTextAttributes = [
                  NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
              ]
          } else {
               if #available(iOS 11, *) {
                   UITabBarItem.appearance().setTitleTextAttributes([
                       NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
                   ], for: .normal)
                   UITabBarItem.appearance().setTitleTextAttributes([
                       NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
                   ], for: .selected)
               }
          }
          

          【讨论】:

            猜你喜欢
            • 2015-09-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-22
            • 2020-10-27
            • 2015-07-04
            • 1970-01-01
            相关资源
            最近更新 更多