【问题标题】:How to change the font and text color of the title of UINavigationBar如何更改 UINavigationBar 标题的字体和文字颜色
【发布时间】:2015-10-29 00:59:09
【问题描述】:

我想把标题的字体改成 Avenir,我想把它变成白色。这是我在viewDidLoad 方法中的代码:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 20)!]
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

此代码仅更改标题的颜色,而不更改字体。此外,我尝试在 App Delegate File 中实现这一点,但没有奏效。

这是我的 UINavigationBar 的样子:

我希望标题也有 Avenir 字体。我如何做到这一点?

【问题讨论】:

    标签: ios swift fonts uinavigationcontroller uinavigationbar


    【解决方案1】:

    看来这也是需要的:

    self.navigationController?.navigationBar.barStyle       = UIBarStyle.Black // I then set the color using:
    
    self.navigationController?.navigationBar.barTintColor   = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0) // a lovely red
    
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() // for titles, buttons, etc.
    
    let navigationTitleFont = UIFont(name: "Avenir", size: 20)!
    
    self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: navigationTitleFont]
    

    【讨论】:

      【解决方案2】:

      我会创建一个插座并这样做:

      @IBOutlet weak var navigationBar: UINavigationBar!
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
      
          navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 30)!]
      }
      

      【讨论】:

      • 感谢您的回复!我以编程方式创建了所有 UI 元素,没有使用 Interface Builder。所以,这个解决方案不适合我的需要。
      【解决方案3】:

      如果您希望在整个应用程序中应用样式,请在 AppDelegate.m 文件中添加这些行。

      NSShadow* shadow = [NSShadow new]; 
      shadow.shadowOffset = CGSizeMake(0.0f, 0.0f); 
      shadow.shadowColor = [UIColor blackColor]; 
      
      [[UINavigationBar appearance] setTitleTextAttributes: @{
      NSForegroundColorAttributeName: [UIColor whiteColor],
      NSFontAttributeName: [UIFont fontWithName:@"Kelvetica Nobis" size:20.0f],
      NSShadowAttributeName: shadow 
      }];
      

      NSForegroundColorAttributeName 设置字体颜色。

      NSFontAttributeName 为标题文本设置自定义字体。

      NSShadowAttributeName 对文本应用了漂亮的阴影效果,如果需要,您可以删除这部分。

      此外,您还可以提前添加这些行以隐藏在导航控制器中导航到另一个视图时出现在操作栏中的后退按钮中的文本。

      [[UIBarButtonItem appearance]
           setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000)
           forBarMetrics:UIBarMetricsDefault];
      

      希望这对您的问题有所帮助:)

      【讨论】:

        【解决方案4】:

        您的代码没问题,您只需在一行中设置所有titleTextAttributes

        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white , NSFontAttributeName: UIFont(name: "Avenir", size: 17)!]
        

        【讨论】:

          【解决方案5】:

          如果有人在 Swift 4 中需要这个:

          let navBarAppearance = UINavigationBar.appearance()
          navBarAppearance.barTintColor = .red
          navBarAppearance.tintColor = .white
          navBarAppearance.titleTextAttributes = [
                  NSAttributedString.Key.foregroundColor: UIColor.white,
                  NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
          

          【讨论】:

          • UINavigationBar.appearance().tintColor 不会影响我在 Xcode 10 和 Swift 5 中的导航标题文本颜色,它们保持黑色。我找不到如何用我的主题更新它们。
          • @Vilmir 你应该使用titleTextAttributes 作为标题。 tintColor 用于 UIBarButtonItems。
          【解决方案6】:

          Swift 5 中:

          let appearance = UINavigationBarAppearance()
              appearance.titleTextAttributes = [
                  NSAttributedString.Key.foregroundColor: UIColor.white,
                  NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
              appearance.largeTitleTextAttributes = [
                  NSAttributedString.Key.foregroundColor: UIColor.white,
                  NSAttributedString.Key.font: UIFont(name: "Avenir", size: 35)!]
          

          此处的代码用于标题和大标题。如果需要系统字体,也可以删除自定义字体。

          【讨论】:

            【解决方案7】:

            我正在为 iOS 15 更新此内容。如果您有一个滚动视图将某些东西推到导航栏后面,如果您不设置“navigationBar.scrollEdgeAppearance”,它将更改导航栏背景。

            所以这是一个简单的配置,可以在主 ViewController 的 ViewDidLoad 中为所有视图设置它。

                    // Set top Nav Bar behavior for ALL of app
                let standardAppearance = UINavigationBarAppearance()
                
                // Title font color
                standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
                
                // prevent Nav Bar color change on scroll view push behind NavBar
                standardAppearance.configureWithOpaqueBackground()
                standardAppearance.backgroundColor = UIColor.blue
                
                
                self.navigationController?.navigationBar.standardAppearance = standardAppearance
                self.navigationController?.navigationBar.scrollEdgeAppearance = standardAppearance
            

            所以“scrollEdgeAppearance”可以设置标题颜色、色调、导航栏颜色和作品。标准外观是加载时显示的内容。

            我花了一段时间才为 iOS15 弄明白这个。

            不客气! :-)

            【讨论】:

              猜你喜欢
              • 2014-07-03
              • 1970-01-01
              • 2015-10-04
              • 1970-01-01
              • 1970-01-01
              • 2012-05-25
              • 1970-01-01
              • 1970-01-01
              • 2015-03-25
              相关资源
              最近更新 更多