iOS更改导航栏标题字体和颜色

111

所以我有这段代码,应该可以更改导航栏标题的字体,但它没有生效。

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
                                                                       fontWithName:_dataManager.optionsSettings.fontString size:14], NSFontAttributeName,
                            [UIColor whiteColor], NSForegroundColorAttributeName, nil];

[[UINavigationBar appearance] setTitleTextAttributes:attributes];

使用这段代码更改返回按钮字体效果很好。

   //set backbutton font
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                  [UIFont fontWithName:_dataManager.optionsSettings.fontString size:15], NSFontAttributeName,
                                  nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes
                                            forState:UIControlStateNormal];
18个回答

2
我遇到了一个问题,因为当我试图通过编程方式更改我的NavigationItem标题时,我无法找到我的家族字体(尝试了许多方法,但无法使它正确运行),所以我在storyboard中找到了一个非常好的和容易的解决方法。
  1. 首先,在导航栏项下添加一个视图,并不要忘记将背景颜色设置为清除颜色,以获得与导航栏相同的颜色:

enter image description here

其英译中为:
  1. 接着,您可以在此视图中添加一个标签(Label),并对其进行编辑(设置文本颜色、字体、大小等)
  2. 您需要将标签添加约束(Top = 0, Bottom = 0, Trailing = 0 和 Leading = 0)到视图,并将标签的文本居中

最终,在文档大纲中应该看起来像这样:

enter image description here

在您的 ViewController 中可能会有类似这样的东西:

enter image description here

希望它能有所帮助。

1
这是关于替代Swift 4的一个选项(已由@Josh回答):
let titleTextAttributed: [NSAttributedStringKey: Any] = [.foregroundColor: UIColor.red, .font: UIFont(name: "AvenirNext-Regular", size: 20) as Any]
navigationController?.navigationBar.titleTextAttributes = titleTextAttributed

1

设置字体和颜色的Objective-C代码

- (void)_setup {
    NSDictionary *barButtonTitleAttributes = @{
                                               NSForegroundColorAttributeName : [UIColor whiteColor],
                                               NSFontAttributeName :[UIFont fontWithName:@"Lato-Regular" size:15.0]
                                               };
    [self.navigationBar setTitleTextAttributes:barButtonTitleAttributes];

}

0
不要忘记添加键的原始值以避免编译错误。
    let textAttributes:[NSAttributedStringKey: Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue):UIColor.blue, NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):UIFont(name:"OpenSans", size: 17)!]
    navigationController?.navigationBar.titleTextAttributes = textAttributes

0

Swift 4.2

self.navigationController?.navigationBar.titleTextAttributes =
        [NSAttributedString.Key.foregroundColor: UIColor.white,
         NSAttributedString.Key.font: UIFont(name: "LemonMilklight", size: 21)!]

0

添加此扩展

extension UINavigationBar {
    func changeFont() {
        self.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Poppins-Medium", size: 17)!]
    }
}

在viewDidLoad()中添加以下行:
self.navigationController?.navigationBar.changeFont()

0

这里是关于Swift 4的答案:

    let textAttributes:[String:Any]? = [NSAttributedStringKey.foregroundColor.rawValue:UIColor.blue, NSAttributedStringKey.font.rawValue:UIFont(name:"Avenir Next", size:20)!]
    navigationController?.navigationBar.titleTextAttributes = textAttributes

-1

使用Swift 3.0进行编程 要更改标题颜色,您需要添加titleTextAttributes,如下所示

        let textAttributes = [NSForegroundColorAttributeName:UIColor.white]
        self.navigationController.navigationBar.titleTextAttributes = textAttributes

For changing navigationBar background color you can use this
        self.navigationController.navigationBar.barTintColor = UIColor.white

For changing navigationBar back title and back arrow color you can use this
       self.navigationController.navigationBar.tintColor = UIColor.white

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接