3

I want to encode a text to HTML and apply multiple attributes at the same time.

My Swift 4 code looks like this:

let attributedString = try? NSMutableAttributedString(
                data: tweetContent.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
                options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
                documentAttributes: nil)

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = 1.4

let attributes = [NSAttributedStringKey.font: UIFont(name: "Roboto-Regular", size: 17.0)!,
                NSAttributedStringKey.paragraphStyle: paragraphStyle]

let textRangeForFont : NSRange = NSMakeRange(0, attributedString!.length)
attributedString?.addAttributes(attributes, range: textRangeForFont)

tweetContent represents the string that I want to set a font for and which should be encoded for HTML. In the end, I want to set as many attributes as I like to that string without loosing the html encoding.

However, this code results in displaying the placeholder text which I set up in the storyboard.

Any help is appreciated!

1 Answer 1

2

I forgot to add the attributed text to the textview:

textView.attributedText = attributedString 

This solved my problem.

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.