Search code examples
iosswiftnsstringnsattributedstring

Convert UItextview to NSAttributedString


I've a UITextView with different properties like different color in the same text, font and size. I need to pass that text into a NSAttributedString variable. With that code I'm not able to keep the properties of the text. How can I do?

  let attr = NSAttributedString(string: textView.text)

Solution

  • Use the attributedString initializer of NSAttributedString and pass it your textview attributed text:

    let attr = NSAttributedString(attributedString: textView.attributedText)