Las*_*sen 7 fonts uinavigationbar ios swift
我想更改导航栏中的字体.但是,以下代码不起作用,它会导致应用程序崩溃.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Lato-Light.ttf", size: 34)!]
return true
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
致命错误: unexpectedly found nil while unwrapping an Optional value(lldb)
我确实已将字体Lato-Light.ttf添加到我的项目中,因此它应该能够找到它.
zis*_*oft 17
UIFont()是一个可用的初始化器,它可能由于几个原因而失败.使用强制解包会!导致您的应用崩溃.
更好地单独初始化并检查是否成功:
if let font = UIFont(name: "Lato-Light.ttf", size: 34) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: font]
}
Run Code Online (Sandbox Code Playgroud)
并检查您的字体文件是否包含在捆绑包资源中.
| 归档时间: |
|
| 查看次数: |
13310 次 |
| 最近记录: |