关于自定义字体的奇怪问题

时间:2015-12-15 03:00:33

标签: ios fonts

我已将字体A添加到我的应用程序中(设置Copy Bundle Resources& .plist)。当我在storyboard中设置Label的字体时,我可以找到字体A,它可以正常显示。

但是当我只在代码中设置Label的字体时,它崩溃了:"致命错误:在解开一个Optional值时意外发现nil"

 self.navigationController?.navigationBar.titleTextAttributes = [
     NSFontAttributeName: UIFont(name: "Shojumaru-Regular", size: 26)!,
     NSForegroundColorAttributeName: UIColor.whiteColor()]

我打印了字体系列,如果我在故事板中设置了字体,它可以打印字体,但如果我只在代码中设置,则无法在fontfamily中找到字体)

Shojumaru: Shojumaru-Regular

1 个答案:

答案 0 :(得分:1)

创建UIFont实例时,您希望使用字体名称而不是字体系列名称对其进行初始化。因此,在您的示例中,您需要使用" Shojumaru-Regular"初始化它。

最终会看起来像这样:

UIFont(name: "Shojumaru-Regular", size: 26)!