【问题标题】:iOS change navigation bar title font and coloriOS更改导航栏标题字体和颜色
【发布时间】:2023-03-09 12:57:01
【问题描述】:

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

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

【问题讨论】:

    标签: ios fonts ios7 navigationbar


    【解决方案1】:

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

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

    斯威夫特 5:

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

    【讨论】:

    • 无论我尝试什么值,大小都保持不变
    • 是的,我意识到我的字体没有正确加载。还是不明白为什么,我用同样的方法导入了 OpenSans-Light 和 OpenSans-Regular,只有 OpenSans-Light 好像可以用……
    • Obj-C 代码缺少右括号。应该是:[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21]}];
    • 转到更改backBarButton字体的链接:stackoverflow.com/a/16538596/5967144
    • 使用 LargeTitle NavBar 时会怎样?我没有找到任何navigationBar.LargeTitleTextAttributes 或任何与此相关的东西。你知道如何全局设置 LargeTitle 字体吗?
    【解决方案2】:

    添加这个扩展

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

    在 viewDidLoad() 中添加以下行

    self.navigationController?.navigationBar.changeFont()
    

    【讨论】:

      【解决方案3】:

      Swift 4.2

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

      【讨论】:

        【解决方案4】:

        在您的 App Delegate 中添加此单行代码 - Did Finish Lauch。它将在整个应用程序中更改导航栏的字体、颜色。

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

        【讨论】:

          【解决方案5】:

          Objective-C 设置字体和颜色

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

          【讨论】:

            【解决方案6】:

            斯威夫特:-

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

            【讨论】:

              【解决方案7】:

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

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

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

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

              注意事项(注意事项)

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

              1. 您确实需要在字体生效之前切换导航栏的色调(这似乎是 Xcode 中的一个错误;您可以将其切换回默认值并且字体会保持不变)
              2. 如果选择系统字体~一定要确保大小不是 0.0(否则会忽略新字体)

              1. 当视图中只有一个 NavBar 时,这似乎没有问题 等级制度。似乎忽略了同一堆栈中的辅助 NavBar。 (请注意,如果您显示主导航控制器的导航栏,则所有其他自定义导航栏设置都将被忽略)。

              陷阱(双人)

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

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

              结果

              样品

              处理自定义字体

              注意~nice checklist 可以在 Code With Chris 网站上找到,您可以查看示例下载项目。

              如果您有自己的字体并想在情节提要中使用它,那么以下SO Question 有一组不错的答案。一个答案确定了这些步骤。

              1. 获取自定义字体文件(.ttf,.ttc)
              2. 将字体文件导入您的 Xcode 项目
              3. 在 app-info.plist 中,添加一个名为 Fonts 的键 application.It 是一个数组类型,将所有字体文件名添加到 数组,注意:包括文件扩展名。
              4. 在情节提要中,在 NavigationBar 上转到 Attribute 检查器,单击字体选择区域的右侧图标按钮。在 弹出面板,选择自定义字体,然后选择你的家人 嵌入字体名称。

              自定义字体解决方法

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

              要解决此问题:

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

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

              【讨论】:

                【解决方案8】:

                不要忘记添加键的原始值以避免编译错误。

                    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
                

                【讨论】:

                  【解决方案9】:

                  iOS 11

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

                  【讨论】:

                    【解决方案10】:

                    这是 Swift 4 的替代品(@Josh 已经回答):

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

                    【讨论】:

                      【解决方案11】:

                      这里是您问题的答案:

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

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

                      【讨论】:

                        【解决方案12】:

                        这是 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】:

                          在 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
                          

                          【讨论】:

                            【解决方案14】:

                            我遇到了一个问题,因为当我尝试以编程方式更改我的 NavigationItem 标题时,我无法找到我的家庭字体(尝试了很多方法但无法使其正确运行),因此我在情节提要中找到了一种非常好的和简单的解决方法。

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

                            1. 然后在此视图中添加一个可以编辑的标签(设置文本颜色、字体、大小...)
                            2. 您将标签的约束(顶部 = 0、底部 = 0、尾随 = 0 和前导 = 0)添加到视图和标签的中心文本

                            最后你应该在文档大纲中有类似的东西:

                            在你的 ViewController 中有类似的东西:

                            希望对你有帮助。

                            【讨论】:

                              【解决方案15】:

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

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

                              【讨论】:

                                【解决方案16】:

                                使用文字更易读:

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

                                【讨论】:

                                  【解决方案17】:

                                  我用于更改导航栏标题的 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)
                                  

                                  【讨论】:

                                    【解决方案18】:

                                    试试这个:

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

                                    【讨论】:

                                      猜你喜欢
                                      • 1970-01-01
                                      • 1970-01-01
                                      • 2014-07-30
                                      • 2016-05-31
                                      • 1970-01-01
                                      • 2017-02-17
                                      • 2017-01-05
                                      • 2023-03-19
                                      • 1970-01-01
                                      相关资源
                                      最近更新 更多