繁体   English   中英

Swift 5中的自定义字体在Xcode 10中不起作用

[英]Custom font in Swift 5 not working in Xcode 10

我试图在Xcode 10中添加自定义字体,我不断收到此错误:

致命错误:在解开Optional值时意外发现nil

  • 我在Info.plist中包含了这些字体
  • 我检查了我的“Copy Bundle Resources”并且它就在那里。

我仍然得到这个错误。

这个我的代码如下:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM