在swift中更改titleTextAttribute

Pet*_*Pik 24 iphone ios appdelegate swift

自从我更新xcode后,我似乎无法改变titleTextAttribute.现在,当我使用以下代码时,我收到此错误:

找不到接受这个提供的参数的重载init

代码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)
Run Code Online (Sandbox Code Playgroud)

Nat*_*ook 40

最近有一个更改,以便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)
Run Code Online (Sandbox Code Playgroud)


Ste*_*ser 8

斯威夫特4:

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]
Run Code Online (Sandbox Code Playgroud)