Search code examples
swiftxcodensmutableattributedstring

Is it possible to assign different attributes to different characters in a string in UITextView?Swift 3


I placed a large text in a UITextView In Xcode in interface builder. Is it possible to assign different attributes (colors, headings, alignments..etc) to different parts of the string. Using MutableAttributedString would not work for me since the string is longer than 5000 words, it would take ages to format it.

enter image description here


Solution

  • There is a simple way to do this. Outside Xcode :)

    Open TextEditor in MAC and paste your text into it and format it as per your requirement and again copy it.

    Now Add your UITextView into your ViewController in Storyboard and make that textView Attributed.

    Now Paste your formatted text there in the text field.

    Check below screenshot for reference.

    enter image description here

    enter image description here

    Hope this will help you.