[英]Custom font in Swift 5 not working in Xcode 10
I am trying to add Custom Fonts in Xcode 10 & I keep getting this error: 我试图在Xcode 10中添加自定义字体,我不断收到此错误:
Fatal error: Unexpectedly found nil while unwrapping an Optional value 致命错误:在解开Optional值时意外发现nil
I still get this error. 我仍然得到这个错误。
This my code below: 这个我的代码如下:
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)
}
Throws Error on this line 在此行引发错误
UIFont (name: "Lato-Regular", size: 14)!
Xcode cannot find font, but I can also use the font on the Storyboard UI. Xcode找不到字体,但我也可以使用Storyboard UI上的字体。 So how come? 那怎么来的?
UIFont.familyNames
will give you a list of all available font families. UIFont.familyNames
将为您提供所有可用字体系列的列表。 UIFont.fontNames(forFamilyName:)
will give you all font names. UIFont.fontNames(forFamilyName:)
将为您提供所有字体名称。 Use that to find out the correct name of your font. 用它来找出字体的正确名称。
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.