自从我更新了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)

最佳答案

最近发生了更改,因此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)

关于ios - 快速更改titleTextAttribute,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26868847/

10-12 00:16