如何在Swift中更改UIBarButtonItem的字体?

时间:2016-01-28 22:33:55

标签: ios swift uibarbuttonitem

我通常是一名耐心的调试人员,花费大量时间使用Google搜索并搜索问题的答案。但是,我的耐心已经用完了,我开始相信这根本不可能。

但是,我正在使用导航栏处理iOS应用程序,并选择使用" Optima"我的整个用户界面的字体。我已经能够更改导航栏标题的字体,以及初始视图中的自定义导航栏按钮。但是,在任何后续视图中,我的后退按钮上的字体默认为系统。对于我的生活,我无法找到改变这种状况的方法。

我已经尝试了搜索中出现的所有内容,而我最近的成功来自this blog post

下面的代码对我有用,但是当然要将我的后退按钮更改为图像,而不是像我希望的那样改变字体

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "Tab_Trophy")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "Tab_Trophy")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

(顺便说一句,上面的代码位于我的后退按钮向用户发送的视图的viewDidLoad()中)

无论如何,我已尝试在不同的地方使用下面的setTitleTextAttributes,但无济于事。

setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Optima", size: 19.0)!], forState: .Normal)

所以我的问题是:可以做到吗?或者我应该使用我想要的字体中的文本制作图像,并使用上面有效的代码吗?

2 个答案:

答案 0 :(得分:1)

以下代码将更改顶部' navbar'的字体。和底部的工具栏' items(UINavigationBar.appearance()。titleTextAttributes仅更改导航栏。)

注意:

  • 使用以下方法设置导航栏字体:

      

    navigationController.navigationBar.titleTextAttributes

  • 它通过循环遍历每个工具栏并使用:

    来设置工具栏项
      

    toolbarItem.setTitleTextAttributes

  • 它在viewDidLayoutSubviews中运行,因此添加到Storyboard工具栏中的项目也会受到影响。如果您以编程方式执行所有操作,则可以将其放在viewDidLoad或viewWillAppear中。
  • boolean firstShow只是为了防止每次用户旋转设备时都运行此代码。不是绝对必要的(如果您将代码移动到如上所述的viewDidLoad / viewWillAppear,则不必要)但效率更高。
  • 字体的展开可能看起来有点迂腐,但如果Apple在未来的iOS版本中撤销您的命名字体,应用程序将会崩溃。
  • 它没有指定按钮栏项目的颜色(这样做会弄乱它们突出显示的/未突出显示的颜色) - 而是使用默认的app色调(例如在Storyboard中)来指定它们的颜色。
  • 它确实为导航栏指定了一种颜色(导航栏标题会忽略应用程序色调,但它不受其按钮中突出显示/未突出显示的问题的影响)。
  • 更改" AmericanTypewriter",颜色并根据需要添加其他属性。
  • 分享并享受。

    var firstShow = false
    
    override func viewDidLayoutSubviews() {
    
        super.viewDidLayoutSubviews()
    
        if firstShow {
    
            let navbarAttributes:[String:Any]
            let toolbarAttributes:[String:Any]
            if let font = UIFont(name: "AmericanTypewriter", size: 22.0) {
                navbarAttributes = [
                    NSFontAttributeName: font,
                    NSForegroundColorAttributeName : UIColor.init(red:1.0, green:0, blue:0, alpha:1.0)
                ]
                toolbarAttributes = [
                    NSFontAttributeName: font
                ]
            }
            else {
                navbarAttributes = [
                    NSForegroundColorAttributeName : UIColor.init(red:1.0, green:0, blue:0, alpha:1.0)
                ]
                toolbarAttributes = [:]
            }
            navigationController?.navigationBar.titleTextAttributes = navbarAttributes
    
            if let toolbarItems = navigationController?.toolbar.items {
                for toolbarItem in toolbarItems {
                    toolbarItem.setTitleTextAttributes(toolbarAttributes, for: .normal)
                    toolbarItem.setTitleTextAttributes(toolbarAttributes, for: .disabled)
                }
            }
    
            firstShow = false
        }
    }
    

答案 1 :(得分:0)

您可以更改应用的所有UIBarButtonItem标签的字体:

UINavigationBar.appearance().titleTextAttributes = [
    NSFontAttributeName: UIFont(name: "Optima", size: 19.0)!
]

或者,如果您只想自定义单个标签,则可以使用以下内容的自定义按钮:

let button = UIButton()
// Customise button as required
let barButtonItem = UIBarButtonItem(customView: button)