ios - 向项目添加自定义字体但是当我尝试使用它们时我得到一个 "fatal error: unexpectedly found nil while unwrapping an Optional value"

标签 ios swift uitextfield nsattributedstring

我向我的项目添加了自定义字体,并确保它位于构建阶段的“复制捆绑资源”中。我还确保它已正确添加到 plist 文件中:plist

但是当我为占位符文本设置一个函数时:

let attributes = [
  NSForegroundColorAttributeName: UIColor.white,
  NSFontAttributeName : UIFont(name: "NeutraText-Bold", size: 17)!
]


func placeHolder(_ text: String) -> (NSAttributedString){
  return NSAttributedString(string: text, attributes:attributes)
}

   signUpTextField.attributedPlaceholder = placeHolder("Sign Up")

我得到错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

为线

  NSFontAttributeName : UIFont(name: "NeutraText-Bold", size: 17)!

最佳答案

如果您使用文件夹引用来保存字体,则需要确保文件夹本身已分配给您的目标。

enter image description here

如果是这样,请标记您拥有的顶级文件夹,并在右侧检查目标成员资格是否标记正确。

关于ios - 向项目添加自定义字体但是当我尝试使用它们时我得到一个 "fatal error: unexpectedly found nil while unwrapping an Optional value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43925353/

相关文章:

ios - Twitter 套件未使用 Fabric 集成

ios - 快速滚动ios时自定义单元格中的UITextField值出现问题

ios - 如果 TextField 为空,则设置自定义文本

ios - 触摸 uitextField 时如何用 uitextView 替换 uitextField (swift 2)

ios - 模拟标签外观的按钮(有点)

ios - 像 Safari 应用程序一样拖动时如何隐藏标签栏?

ios - Flutter check if app was in memory when launched (How to check if splash screen is shown in iOS)

ios - 如何根据 UICollectionView 内容大小设置 UITableViewCell 高度?

iOS 10 快速查看内存使用情况

ios - 如何在 swift 中只运行一次代码?