【问题标题】:Change BarButtonItem Font Using Swift (Xcode 6)使用 Swift (Xcode 6) 更改 BarButtonItem 字体
【发布时间】:2015-02-18 16:07:00
【问题描述】:

我在更改已添加到导航控制器的 BarButtonItem 的 swift Xcode 项目中的字体时遇到问题。我能够毫无问题地更改按钮的颜色,但字体不会改变。代码:

var navTextColor = UIColor(red:0.3, green:0.09, blue:0.05, alpha:1.0)
self.navigationController?.navigationBar.tintColor = navTextColor

【问题讨论】:

    标签: xcode swift fonts navigation uinavigationbar


    【解决方案1】:

    如果您创建并链接到您的UIBarButtonItem 的插座(例如@IBOutlet var barButton: UIBarButtonItem!),您应该能够通过在插座上使用setTitleTextAttributes 来更改您的字体类型。

    barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], forState: UIControlState.Normal)
    

    Swift3

    barButton.setTitleTextAttributes([ NSFontAttributeName: UIFont(name: "Arial", size: 12)!], for: UIControlState.normal)
    

    【讨论】:

    • 这会更改导航栏文本,但不会更改添加到其中的栏按钮项的文本。
    • 确实如此。我编辑了答案以更改按钮本身的字体
    【解决方案2】:

    斯威夫特 3

    另一种更改所有TabBarItems 字体的简单方法是在ViewDidLoad() of UITabBarController 中使用此代码:(无需创建Outlet)

    UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)
    

    【讨论】:

      【解决方案3】:

      斯威夫特 5

      barButton.setTitleTextAttributes([ NSAttributedString.Key.font: UIFont(name: "Arial", size: 12)!], for: UIControl.State.normal)
      

      【讨论】:

        猜你喜欢
        • 2014-11-05
        • 2014-10-07
        • 1970-01-01
        • 1970-01-01
        • 2018-05-07
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多