iOS 更改导航栏标题字体和颜色 [英] iOS change navigation bar title font and color

查看:269
本文介绍了iOS 更改导航栏标题字体和颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个代码应该改变导航栏标题字体,但它确实

So i have this code that should change the nav bar title font, but it doenst

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

[[UINavigationBar appearance] setTitleTextAttributes:attributes];

使用此代码更改后退按钮字体效果很好.

Changing the back button font with this code works just fine.

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

推荐答案

更改标题字体(和颜色)的正确方法是:

The correct way to change the title font (and color) is:

[self.navigationController.navigationBar setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21]}];

Swift 4.2

self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.red,
 NSAttributedString.Key.font: UIFont(name: "mplus-1c-regular", size: 21)!]

Swift 4

self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedStringKey.foregroundColor: UIColor.red,
 NSAttributedStringKey.font: UIFont(name: "mplus-1c-regular", size: 21)!]

Swift 3:

self.navigationController?.navigationBar.titleTextAttributes = 
[NSForegroundColorAttributeName: UIColor.redColor(),
 NSFontAttributeName: UIFont(name: "mplus-1c-regular", size: 21)!]

Swift 5:

navigation.navigationBar.titleTextAttributes = [
    .foregroundColor: UIColor.red,
    .font: UIFont(name: "mplus-1c-regular", size: 21)!
]

这篇关于iOS 更改导航栏标题字体和颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆