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

标签 ios fonts ios7 navigationbar

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

    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];

最佳答案

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

[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更改导航栏标题字体和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19791762/

相关文章:

ios - 在自定义转换中获取 nil [transitionContext containerView]

iphone - xcode 4 第二个目标?

ios - Parse 中要删除的对象太多,阻止 UI : Doesnt respond, 取消删除

html - Fonts.com 字体不会居中

html - FireFox 无法加载字体 404 Not Found

ios - 使用 AVCaptureDevice 读取条形码,我想在扫描后立即显示动画 ActivityIndi​​cator

ios7 - 在 ios7 模拟器上使用美元符号 ($) 作为密码

UIWebView 中的 HTML 表格宽度

iphone - 按下按钮时切换 View

fonts - OpenType/TrueType 中复合字形的组成部分