[英]Custom font in Swift 5 not working in Xcode 10
我試圖在Xcode 10中添加自定義字體,我不斷收到此錯誤:
致命錯誤:在解開Optional值時意外發現nil
我仍然得到這個錯誤。
這個我的代碼如下:
DispatchQueue.main.async {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attributedString = NSAttributedString(string: message, attributes:
[.paragraphStyle: paragraph, NSAttributedString.Key.font : UIFont (name: "Lato-Regular", size: 14)!]) // Throws error here
let alert = UIAlertController(title: title, message: "", preferredStyle: .alert)
alert.setValue(attributedString, forKey: "attributedMessage")
alert.addAction(UIAlertAction(title: "Back", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
在此行引發錯誤
UIFont (name: "Lato-Regular", size: 14)!
Xcode找不到字體,但我也可以使用Storyboard UI上的字體。 那怎么來的?
UIFont.familyNames
將為您提供所有可用字體系列的列表。 UIFont.fontNames(forFamilyName:)
將為您提供所有字體名稱。 用它來找出字體的正確名稱。
let families = UIFont.familyNames
let allFonts = families.flatMap { UIFont.fontNames(forFamilyName:$0) }
let latoFonts = allFonts.filter { $0.contains("Lato") }
print(latoFonts)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.