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

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

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

用这个代码改变后退button的字体工作得很好。

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

迅速:

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

其他答案没有错。 我只是分享设置字体的故事板版本。

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

导航栏

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

标题字体

(在Xcode拾取新字体之前,您可能需要切换导航栏的条形色调)

注意事项(警告)

validation这在Xcode 7.1.1+上工作。 ( 请参阅下面的示例

  1. 您需要在字体生效之前切换导航栏的色调(看起来像是Xcode中的一个错误;您可以将其切换回默认值,字体将保持不变)
  2. 如果您select了系统字体〜请务必确保大小不为0.0(否则,新字体将被忽略)

尺寸

  1. 当只有一个NavBar位于视图层次结构中时,似乎可以正常工作。 似乎在同一个堆栈中的次要NavBars被忽略。 (请注意,如果您显示主导航控制器的导航栏,则忽略所有其他自定义导航栏设置。

陷阱(deux)

其中一些是重复的,这意味着他们很可能值得注意。

  1. 有时故事板xml被损坏。 这要求您在Storyboard模式下以源代码模式查看结构(右键单击故事板文件>打开为…)
  2. 在某些情况下,与用户定义的运行时属性相关联的navigationItem标记被设置为视图标记的xml子代,而不是视图控制器标记。 如果是这样的标签之间的正确操作。
  3. 切换NavBar Tint以确保使用自定义字体。
  4. validation字体的大小参数,除非使用dynamic字体样式
  5. 视图层次结构将覆盖设置。 看来每个堆栈可能有一种字体。

结果

导航栏斜体

样品

  • 在高级项目中显示多个字体的video
  • 简单的来源下载
  • 高级项目下载〜显示多个NavBar字体和自定义字体的解决方法
  • 显示多种字体和自定义字体的video

处理自定义字体

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

如果你有自己的字体,并想在故事板中使用它,那么在下面的SO问题上有一个相当不错的答案。 一个答案标识这些步骤。

  1. 让你自定义字体文件(.ttf,.ttc)
  2. 将字体文件导入到Xcode项目中
  3. 在app-info.plist中,添加一个名为Fonts提供的键。它是一个数组types,将所有字体文件名添加到数组中,注意:包括文件扩展名。
  4. 在故事板中,在导航栏上转到属性检查器,单击字体select区域的右侧图标button。在popup式面板中,select要自定义的字体,然后select您embedded的字体名称。

自定义字体解决方法

所以Xcode自然看起来像它可以处理UINavigationItem上的自定义字体,但该function只是没有正确更新(select的字体将被忽略)。

UINavigationItem

要解决这个问题:

一种方法是修复使用故事板,并添加一行代码:首先添加一个UIView(UIButton,UILabel,或其他一些UIView子类)到视图控制器(不是导航项目… Xcode目前不允许做一个那)。 添加控件后,您可以修改故事板中的字体,并将引用添加为视图控制器的出口。 只需将该视图分配给UINavigationItem.titleView。 如有必要,您也可以在代码中设置文本名称。 报告错误(23600285)。

 @IBOutlet var customFontTitleView: UIButton! //Sometime later... self.navigationItem.titleView = customFontTitleView 

尝试这个:

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

我的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) 

使用文字可读性更强一些:

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

任何人都需要一个Swift 3版本。 redColor()已经变成了red

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

我有一个问题,因为当我试图以编程方式更改我的NavigationItem标题,我无法find我的家庭字体(尝试了很多事情,但不可能使其正确运行),所以我发现一个解决scheme非常好,很容易在故事板。

  1. 首先,您可以在导航项目的中间添加一个视图,不要忘记设置backGroundColor以清除颜色,使其具有相同的navBar颜色:

在这里输入图像说明

  1. 然后在这个视图中添加一个你可以编辑的标签(设置文本的颜色,字体,大小…)
  2. 您添加约束标签(顶部= 0,底部= 0,尾随= 0和领导= 0)查看和中心标签文本

最后在文档大纲中应该有这样的东西:

在这里输入图像说明

在你的ViewController里有这样的东西:

在这里输入图像说明

希望它可以帮助。

这是Swift 4 answer的答案:

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

这是你的问题的答案:

将代码移到下面的方法,因为导航栏标题在视图加载后更新。 我试图在viewDidLoad中添加上面的代码不起作用,它在viewDidAppear方法中工作正常。

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

在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