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

时间:2013-11-05 14:49:53

标签: 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];

17 个答案:

答案 0 :(得分:223)

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

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

斯威夫特3:

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

答案 1 :(得分:50)

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

1。在导航控制器中选择导航栏

navbar

2。更改“属性”检查器中的“标题字体”

title-font

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

注释(警告)

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

  1. 您需要在字体生效之前切换导航栏色调(看起来像Xcode中的错误;您可以将其切换回默认值并且字体会粘住)
  2. 如果您选择系统字体〜请务必确保尺寸不是 0.0(否则将忽略新字体)
  3. size

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

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

      1. 有时故事板xml会损坏。这需要你 将Storyboard中的结构作为源代码模式进行检查(右键单击 故事板文件>打开作为...)
      2. 在某些情况下,与用户定义的运行时属性关联的navigationItem标记设置为xml 视图标记的子项而不是视图控制器标记。如果是这样 从标签之间移除它以便正常操作。
      3. 切换导航栏色调以确保自定义字体 使用。
      4. 除非使用动态字体,否则请验证字体的大小参数 式
      5. 查看层次结构将覆盖设置。看来是一种字体 每堆可以。
      6. 结果

        navbar-italic

        样品

        处理自定义字体

        注意〜可以从Code With Chris网站找到nice checklist,您可以看到示例下载项目。

        如果您有自己的字体并希望在故事板中使用它,那么下面的SO Question会有一套不错的答案。一个答案确定了这些步骤。

        1. 获取自定义字体文件(.ttf,.ttc)
        2. 将字体文件导入Xcode项目
        3. 在app-info.plist中,添加一个名为Fonts的密钥 application.It是一个数组类型,将所有的字体文件名添加到 数组,注意:包括文件扩展名。
        4. 在故事板中,在NavigationBar上转到属性 检查器,单击字体选择区域右侧的图标按钮 弹出面板,选择Font to Custom,然后选择你的Family 嵌入字体名称。
        5. 自定义字体解决方法

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

          UINavigationItem

            

          要解决此问题:

               

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

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

答案 2 :(得分:21)

试试这个:

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

答案 3 :(得分:11)

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

答案 4 :(得分:4)

以下是您的问题的答案:

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

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

答案 5 :(得分:3)

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

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

答案 6 :(得分:2)

使用文字更具可读性:

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

答案 7 :(得分:2)

  

迅速:-

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

答案 8 :(得分:2)

在您的应用程序委托中添加此单行代码-是否完成Lauch。它将更改整个应用程序中导航栏的字体,颜色。

UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont(name: "YOUR FONT NAME", size: 25.0)!]

答案 9 :(得分:1)

这个替代Swift 4(已经由@Josh回答):

let titleTextAttributed: [NSAttributedStringKey: Any] = [.foregroundColor: UIColor.red, .font: UIFont(name: "AvenirNext-Regular", size: 20) as Any]
navigationController?.navigationBar.titleTextAttributes = titleTextAttributed

答案 10 :(得分:1)

iOS 11

enter image description here

目标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
}

答案 11 :(得分:1)

要让Objective-C设置字体和颜色

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

}

答案 12 :(得分: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

答案 13 :(得分:0)

不要忘记添加密钥的Raw值以避免编译错误。

    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

答案 14 :(得分:0)

Swift 4.2

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

答案 15 :(得分:0)

添加此扩展名

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

在viewDidLoad()中添加以下行

self.navigationController?.navigationBar.changeFont()

答案 16 :(得分:-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