【问题标题】:Cannot Set Navigation Bar Font in Swift on Latest Xcode Version无法在最新 Xcode 版本的 Swift 中设置导航栏字体
【发布时间】:2014-10-24 21:30:43
【问题描述】:

以下代码在更新到 Xcode 6.1 以跟上 iOS 8.1 之前运行良好:

override func viewDidAppear(animated: Bool) {  
     self.navigationController?.navigationBar.topItem?.title = "Home"
     self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34),  NSForegroundColorAttributeName: UIColor.whiteColor()]
}

这个问题专门在NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)

我在这里遇到的错误是:

! "Could not find an overload for 'init' that accepts the supplied arguments"

我在另一个 StackOverflow 问题上找到了此原始代码,并且在此更新之前它按预期工作(昨天下载了它)。我的字体确实安装正确。

我现在应该以不同的方式编写这段代码,还是有一种全新的方式来设置我的导航栏字体?

谢谢!

【问题讨论】:

    标签: ios xcode swift uinavigationcontroller navbar


    【解决方案1】:

    哎呀。这是我自己想出来的:

    我在声明 NSFontAttributeName 之后需要一个感叹号,因为它需要一个类型“NSString!”。也许它以前只需要一个“NSString”,但我现在没有问题。

    工作线:

    NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 24)!
    

    工作完整代码:

    override func viewDidAppear(animated: Bool) {  
         self.navigationController?.navigationBar.topItem?.title = "Home"
         self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!,  NSForegroundColorAttributeName: UIColor.whiteColor()]
    }
    

    现在似乎是一个愚蠢的问题。希望这对其他人有帮助!

    【讨论】:

      【解决方案2】:

      您正在使用 UIFont(name: 初始化器,因为它被定义为

      init?(name fontName: String, size fontSize: CGFloat) -> UIFont

      failable intializer 从链接中阅读更多内容。因此它返回可选。您需要打开它,因为它需要AnyObject 不是可选的。

       self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34)!,  NSForegroundColorAttributeName: UIColor.whiteColor()]
      

      【讨论】:

        【解决方案3】:

        谢谢本吉!!!

        我稍作改动,并将其应用于导航控制器的外观属性。

            var navigationBarAppearance = UINavigationBar.appearance()
        
            navigationBarAppearance.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "OpenSans", size: 16)!,  NSForegroundColorAttributeName: UIColor.whiteColor()]
        

        【讨论】:

        • 在 Swift 4.2 中:navigationBarAppearance.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "Chalkduster", size: 24.0)!, NSAttributedString.Key.foregroundColor: UIColor.white]
        【解决方案4】:

        最好将你的字体声明为有条件的,像这样:

         if let font = UIFont (name: "AdobeArabic-BoldItalic", size: 20) {
                    UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
                }
        

        这样做,确保您的字体已经找到,我已经安装了新字体,并且在没有条件 if 的情况下使用它时,它会发出一个异常,即解开可选字体。

        【讨论】:

          【解决方案5】:
          override func viewWillLayoutSubviews() {
          
              self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName : UIFont(name: "Roboto-Regular", size: 14)!]
          }
          

          【讨论】:

            【解决方案6】:

            斯威夫特 4.2

            更改小标题 当布局向下滚动时

            override func viewDidAppear(animated: Bool) {  
                 self.navigationController?.navigationBar.topItem?.title = "Home"
                 self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34),  NSForegroundColorAttributeName: UIColor.whiteColor()]
            }
            

            更改大标题

            override func viewDidAppear(animated: Bool) {  
                 self.navigationController?.navigationBar.topItem?.title = "Home"
                 self.navigationController?.navigationBar.largeTitleTextAttributes = [ NSFontAttributeName: UIFont(name: "Lobster 1.4", size: 34),  NSForegroundColorAttributeName: UIColor.whiteColor()]
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-06-04
              • 1970-01-01
              • 1970-01-01
              • 2016-08-11
              • 1970-01-01
              • 1970-01-01
              • 2014-12-23
              • 1970-01-01
              相关资源
              最近更新 更多