简体   繁体   中英

Added custom font to project but when I try and use them I get a “fatal error: unexpectedly found nil while unwrapping an Optional value”

I added a custom font to my project and made sure it was in the "Copy Bundle Resources" in Build Phases. I also made sure it was added properly to the plist file: plist中

but when I set up a function for placeholder text :

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")

I get the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

for the line

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

If you are using folder reference to keep your fonts, you need to make sure that folder itself is assigned to your target.

在此处输入图片说明

If you case, mark the top folder you have, and on the right side, check if Target Membership is marked correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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