0
    let s = NSAttributedString(string: "Percentage", attributes: [NSFontAttributeName : UIFont(name : "Avenir Next Condensed", size : 20), NSUnderlineStyleAttributeName : NSUnderlineStyle.byWord])
    textView.attributedText = s

I get the following error for the above code: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue _isDefaultFace]: unrecognized selector sent to instance 0x608000046930'

If I change the NSFontAttributeName to UIFont.boldSystemFont(ofSize: 20), I can see bold text. Also on adding NSUnderlineStyleAttributeName, I don't see any text at all. How can I fix this?

1
  • let font = UIFont(name : "Avenir Next Condensed", size : 20) returns nil? Try with AvenirNextCondensed-Regular instead for the name (you have to use the PostScript name) Commented Oct 10, 2016 at 10:51

1 Answer 1

2

Two things:

  • You cannot pass Optional value to where non-Null id value is needed.

The attributes: parameter is internally converted to NSDictionary, where its values cannot be nil. But UIFont.init(name:size:) is a faileable initializer, so its return type is Optional. In Swift 3.0.0, Swift generates an instance of type _SwiftValue when converting it to non-Null id. And store it in the attributes. Which is completely useless in the Objective-C side. (This happens even if the actual value is not nil.)

(Swift 3.0.1 improved some parts of this situation.)

  • You cannot pass Swift enums to where id value is needed.

NSUnderlineStyle.byWord is a Swift enum. In Swift 3,Swift generates an instance of type _SwiftValue when converting it to id.

(Swift 3.0.1 has not improved about this situation.)

To fix the two above things, you need to write something like this:

if let font = UIFont(name: "Avenir Next Condensed", size: 20) {
    let s = NSAttributedString(string: "Percentage", attributes: [NSFontAttributeName: font, NSUnderlineStyleAttributeName: NSUnderlineStyle.byWord.rawValue])
    textView.attributedText = s
}
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.