0

I create an extension where I set custom font

extension UILabel {
    var substituteFontName : String {
        get { return self.font.fontName }
        set {
              self.font = UIFont(name: newValue, size: self.font.pointSize)
        }
    }
}

but I have a problem: I get a error Unexpectedly found nil while unwrapping an Optional value, but if I set static size like this

self.font = UIFont(name: newValue, size: 13)

I didn't get a error and fonts is changing. How can I set exactly size which was automatically

1
  • in set you have to use size: self.font?.pointSize ?? UIFont.labelFontSize Commented Apr 18, 2020 at 15:10

1 Answer 1

1

The font attribute is actually implicitly unwrapped UIFont!, so you should change your set to:

self.font = UIFont(name: newValue, size: self.font?.pointSize ?? UIFont.labelFontSize)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.