【问题标题】:How can I change the default font for UIBarButtonItems?如何更改 UIBarButtonItems 的默认字体?
【发布时间】:2019-10-23 15:02:46
【问题描述】:

我想更改所有 UIBarButtonItems 的默认字体。我的应用程序的根视图控制器中有以下代码:

    let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 30)]
    UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)
    UINavigationBar.appearance().titleTextAttributes = attributes
    UINavigationBarAppearance().buttonAppearance.normal.titleTextAttributes = attributes
    UIBarButtonItemAppearance().normal.titleTextAttributes = attributes
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "foo", style: .plain, target: nil, action: nil)

尽管外观发生了变化,但该条形按钮项的字体仍然是默认字体。如何设置默认字体?我知道我可以为每个单独的条形按钮项设置字体,但我正在寻找一种方法来广泛更改它。

【问题讨论】:

    标签: ios fonts uibarbuttonitem


    【解决方案1】:

    iOS 13 之前

    class AppDelegate : NSObject, UIApplicationDelegate {
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    
            if #available(iOS 13, *) {
    
            }else{
                let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 30)]
                UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)
                UINavigationBar.appearance().titleTextAttributes = attributes
            }
    
            return true
        }
    }
    

    iOS 13

    class MyNavigationController : UINavigationController {
    
        override init(rootViewController: UIViewController) {
            super.init(rootViewController: rootViewController)
            if #available(iOS 13, *) {
                let attributes: [NSAttributedString.Key: Any] = [.font: UIFont.systemFont(ofSize: 30)]
                let appearance = UINavigationBarAppearance()
                appearance.buttonAppearance.normal.titleTextAttributes = attributes
                appearance.titleTextAttributes = attributes
                self.navigationBar.standardAppearance = appearance
            }
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 UINavigationBarAppearance 自定义所有导航栏,但您需要在导航栏的外观代理上设置 standardAppearance - 调用如下:

      UINavigationBarAppearance().buttonAppearance.normal.titleTextAttributes = attributes
      UIBarButtonItemAppearance().normal.titleTextAttributes = attributes
      

      孤立地什么也不做,因为他们创造了一个新的外观,设置一个值,然后把它扔掉。而是这样做:

      let appearance = UINavigationBarAppearance()
      appearance.buttonAppearance.normal.titleTextAttributes = attributes
      UINavigationBar.appearance().standardAppearance = appearance
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-23
        • 1970-01-01
        • 1970-01-01
        • 2023-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-14
        相关资源
        最近更新 更多