【问题标题】:Changing titleTextAttribute in swift快速更改titleTextAttribute
【发布时间】:2015-01-08 05:38:48
【问题描述】:

自从我更新 xcode 以来,我似乎无法更改 titleTextAttribute。现在,当我使用以下代码时,出现此错误:

找不到接受这个提供的参数的重载初始化

appDelegate中的代码:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Ubuntu", size: 17), NSForegroundColorAttributeName:UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Ubuntu-Light", size: 15), NSForegroundColorAttributeName:UIColor.whiteColor()], forState: UIControlState.Normal)

【问题讨论】:

    标签: ios iphone swift appdelegate


    【解决方案1】:

    最近有一个变化,UIFont(name:size:) 返回一个可选的UIFont 实例。你需要解开它们才能让它工作。使用! 是最简单的方法,但如果字体不在系统上,则会导致崩溃。尝试类似:

    let navbarFont = UIFont(name: "Ubuntu", size: 17) ?? UIFont.systemFontOfSize(17)
    let barbuttonFont = UIFont(name: "Ubuntu-Light", size: 15) ?? UIFont.systemFontOfSize(15)
    
    UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: navbarFont, NSForegroundColorAttributeName:UIColor.whiteColor()]
    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: barbuttonFont, NSForegroundColorAttributeName:UIColor.whiteColor()], forState: UIControlState.Normal)
    

    【讨论】:

    【解决方案2】:
        if let navFont = UIFont(name: "HelveticaNeue-Bold", size: 30.0) {
            let navBarAttributesDictionary: [NSObject: AnyObject]? = [
                NSForegroundColorAttributeName: UIColor.blackColor(),
                NSFontAttributeName: navFont
            ]
            navigationController?.navigationBar.titleTextAttributes = navBarAttributesDictionary
        }
    

    【讨论】:

    • 必须将 [NSObject:AnyObject] 更改为 [String: AnyObject]
    【解决方案3】:

    对于 Swift 3,您可以尝试以下方法:

    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    

    【讨论】:

      【解决方案4】:

      斯威夫特 4:

      if let navFont = UIFont(name: "Futura", size: 18) {
         let navBarAttributesDictionary: [NSAttributedStringKey: Any] = [
         NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor(netHex: Colors.BabyBlue.rawValue),
         NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): navFont ]
         UINavigationBar.appearance().titleTextAttributes = navBarAttributesDictionary
      }
      

      【讨论】:

      • 你为什么要使用所有这些 rawValues?例如只需键入 NSAttributedStringKey.foregroundColor 即可,无需通过 (rawValue:) 启动它。
      【解决方案5】:

      斯威夫特 4:

      UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
      UINavigationBar.appearance().titleTextAttributes = [
                  NSAttributedStringKey.foregroundColor: UIColor.white
              ]
      

      【讨论】:

      • AppDelegate.shared.navigationController!.navigationBar.barTintColor = UIColor.white AppDelegate.shared.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]
      【解决方案6】:

      斯威夫特 5

      navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:titleColor]
      

      【讨论】:

        猜你喜欢
        • 2015-10-28
        • 2016-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        • 2017-05-12
        相关资源
        最近更新 更多