如何改变UINavigationItem字体?

时间:2015-03-01 16:07:20

标签: ios swift uinavigationitem

我正在尝试更改font的{​​{1}}属性。

我尝试过使用UINavigationItem,但不知怎的,我只能更改标题字体。

如何更改字体或UINavigationItem?例如,后面titleTextAttributes的“更多”文字如下所示:

enter image description here

我尝试过使用:

UIButton

但它只会更改图片上显示的内容,而不会更改“更多”按钮的字体。

4 个答案:

答案 0 :(得分:10)

您的方法无法正常工作的原因是因为您在使用titleTextAttributes = ...时只使用setTitleTextAttributes:forState:我使用此功能为我的整个项目全局自定义导航栏: / p>

func customizeNavBar(#color: UIColor, titleFont: UIFont, buttonFont: UIFont) {

    UINavigationBar.appearance().tintColor = color
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: color, NSFontAttributeName: titleFont]
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal)
}

对于单个实例,请调用相同的函数:

someBarButton.tintColor.setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal)

答案 1 :(得分:0)

Swift 4

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)

答案 2 :(得分:0)

对于iOS 13和Swift5。

用于设置条形按钮项目的文本颜色和字体:

UIBarButtonItem.appearance().setTitleTextAttributes([
    .foregroundColor: UIColor.white, 
    .font: UIFont(name: GetFontNameBold(), size: 40)! ],
    for: UIControl.State.normal)

要设置标题颜色和字体,只需在 viewDidLoad()中添加以下行:

UINavigationBar.appearance().titleTextAttributes = [
    .foregroundColor: UIColor.white, 
    .font: UIFont(name: "Arial", size: 24)! ]

答案 3 :(得分:0)

如果有出口,请执行以下操作:

titleItem.titleLabel.font = UIFont(name: NAME, size: SIZE)