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个回答

263

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

[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)!
]

3
无论我尝试什么值,尺寸都保持不变。 - Raphael Royer-Rivard
1
是的,我意识到我的字体没有正确加载。但我仍然无法弄清楚为什么,我以相同的方式导入了OpenSans-Light和OpenSans-Regular,只有OpenSans-Light似乎可用... - Raphael Royer-Rivard
1
Obj-C代码缺少一个闭合括号。应该是:[self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21]}]; - Dylan Hand
点击以下链接更改返回按钮字体:https://dev59.com/P2Qn5IYBdhLWcg3w1aBf#16538596 - Soorej Babu
使用LargeTitle NavBar时怎么办?我找不到navigationBar.LargeTitleTextAttributes或类似的东西。你知道如何全局设置LargeTitle字体吗? - iKK

53
"我只是分享一个故事版版本来设置字体,其他答案没有问题。"

1. 在导航控制器中选择您的导航栏

navbar

2. 在属性检查器中更改标题字体

title-font

在 Xcode 选取新字体之前,您可能需要切换导航栏的 Bar Tint。

注意事项

已验证此方法适用于 Xcode 7.1.1+。(请参阅下面的示例

  1. 在字体生效前,您需要先切换导航栏的 Bar Tint(这似乎是 Xcode 的一个 bug;您可以将其切换回默认值,字体仍将保留)。
  2. 如果选择系统字体~请确保大小不为 0.0(否则将忽略新字体)。

size

  1. 当视图层次结构中只有一个NavBar时,似乎这个方法可以正常工作。但是在同一堆栈中的辅助NavBars将被忽略。(请注意,如果显示主导航控制器的navBar,则所有其他自定义navBar设置都将被忽略)。

需要注意的问题(二)

其中一些重复了,这意味着它们很可能值得注意。

  1. 有时候故事板XML会损坏。这需要您在Storyboard as Source Code模式下查看结构(右键单击storyboard文件 > Open As ...)
  2. 在某些情况下,与用户定义的运行时属性相关联的navigationItem标记被设置为view标记的xml子级,而不是viewController标记。如果是这样,请从<view>标记之间删除它以进行正确操作。
  3. 切换NavBar Tint以确保使用自定义字体。
  4. 验证字体的大小参数,除非使用动态字体样式
  5. 视图层次结构将覆盖设置。似乎每个堆栈只能使用一种字体。

结果

navbar-italic

示例

处理自定义字体

注意~可以在Code With Chris网站上找到一个很好的清单,您可以查看示例下载项目。

如果您有自己的字体,并想在Storyboard中使用它,那么以下SO Question上有一系列不错的回答。其中一个回答指出了以下步骤:
  1. 获取您的自定义字体文件(.ttf,.ttc)
  2. 将字体文件导入到Xcode项目中
  3. 在app-info.plist中添加一个名为“Fonts provided by application”的键。它是一个数组类型,将所有字体文件名添加到数组中,注意:包括文件扩展名。
  4. 在Storyboard中,在NavigationBar上转到Attribute Inspector,在Font选择区域的右侧图标按钮上单击。在弹出面板中,选择Font to Custom,并选择您嵌入字体名称的Family。

自定义字体解决方法

Xcode自然看起来可以处理UINavigationItem上的自定义字体,但该功能未能正确更新(所选字体被忽略)。

UINavigationItem

解决方法:一种方法是使用Storyboard修复并添加一行代码:首先向视图控制器(而不是导航项...Xcode目前不允许这样做)添加一个UIView(UIButton,UILabel或其他UIView子类)。添加控件后,您可以在Storyboard中修改字体并将其作为引用添加到您的视图控制器中。只需将该视图分配给UINavigationItem.titleView即可。如果需要,还可以在代码中设置文本名称。报告的Bug(23600285)。
@IBOutlet var customFontTitleView: UIButton!

//Sometime later...    
self.navigationItem.titleView = customFontTitleView

20

试一试:

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor whiteColor],NSForegroundColorAttributeName,
                                [UIColor whiteColor],NSBackgroundColorAttributeName,nil];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;

14

用于更改导航栏标题的Swift代码:

let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = attributes

如果您还想更改背景字体,那么我在我的AppDelegate中有以下代码:

let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)

9
在你的应用程序委托 - 完成启动时,添加以下单行代码。它将在整个应用程序中更改导航栏的字体和颜色。
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont(name: "YOUR FONT NAME", size: 25.0)!]

5

这里是您的问题的答案:

将您的代码移至下方的方法中,因为导航栏标题在视图加载后更新。我尝试将上述代码添加到viewDidLoad中,但不起作用,它在viewDidAppear方法中运行良好。

  -(void)viewDidAppear:(BOOL)animated{}

3

有人需要Swift 3版本。 redColor() 已更改为 red

self.navigationController?.navigationBar.titleTextAttributes =
        [NSForegroundColorAttributeName: UIColor.red,
         NSFontAttributeName: UIFont(name: "{your-font-name}", size: 21)!]

2

iOS 11

enter image description here

Objective-C

if (@available(iOS 11.0, *)) {
    self.navigationController.navigationItem.largeTitleDisplayMode =  UINavigationItemLargeTitleDisplayModeAlways;
    self.navigationController.navigationBar.prefersLargeTitles = true;

// Change Color
    self.navigationController.navigationBar.largeTitleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};

} else {
    // Fallback on earlier versions
}

2
Swift:-
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Love Nature", size: 40)!]

2

使用文字字面量可以使内容更易读:

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

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