简体   繁体   English

如何更改 UINavigationItem 字体?

[英]How to change UINavigationItem font?

I am trying to change font property for UINavigationItem .我正在尝试更改UINavigationItem font属性。

I've tried using titleTextAttributes but somehow I am only able to change title font.我试过使用titleTextAttributes但不知何故我只能更改标题字体。

How can I change the font or UINavigationItem ?如何更改字体或 UINavigationItem ? For example, the "More" text for the Back UIButton shown below:例如,返回UIButton的“更多”文本如下所示:

在此处输入图片说明

I've tried using:我试过使用:

self.navigationController?.navigationBar.titleTextAttributes = [
            NSForegroundColorAttributeName: UIColor.blackColor(),
            NSFontAttributeName: UIFont(name: "Papyrus", size: 18)!
        ]

but it only changes what is shown on the picture, not the "More" Button's Font.但它只会改变图片上显示的内容,而不是“更多”按钮的字体。

The reason your method wasn't working is because you were just using titleTextAttributes = ... when you have to use setTitleTextAttributes:forState: I use this function to customize the Nav Bar globally for my entire project:您的方法不起作用的原因是因为您只是使用titleTextAttributes = ...当您必须使用setTitleTextAttributes:forState:我使用此函数为我的整个项目全局自定义导航栏:

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)
}

For a single instance, call the same functions:对于单个实例,调用相同的函数:

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

斯威夫特 4

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

For iOS 13 and Swift 5.对于 iOS 13 和 Swift 5。

For setting the bar button item text color & font:用于设置条形按钮项文本颜色和字体:

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

For setting the title color & font just add in viewDidLoad() the following line:要设置标题颜色和字体,只需在viewDidLoad() 中添加以下行:

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

如果您有插座,请执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM