Fry
Fry

Reputation: 6275

iOS put HTML text in UILabel

I need to put HTML text with bold,italic and links in a UILabel. If I use NSAttributedString the links aren't clickable. If I use TTTAttributedLabel links are clickable but the formatting (bold and italic) is not visibile.

This problem drive me crazy! Ideas?

Upvotes: 0

Views: 655

Answers (1)

Mathew Spolin
Mathew Spolin

Reputation: 371

You can make the link formatting work using TTTAttributedLabel. For example, this will color links blue and underline them (in Swift):

let bodyLabel: TTTAttributedLabel

let content = "TTTAttributedLabel will detect http://example.com"
let attributedContent = NSMutableAttributedString(string: content)

// add some attributes as you need to the attributed string, then...

let blueLinks = [NSForegroundColorAttributeName: UIColor.blueColor()]

bodyLabel.enabledTextCheckingTypes = NSTextCheckingType.Link.rawValue
bodyLabel.linkAttributes = blueLinks

bodyLabel.setText(attributedContent)

Upvotes: 0

Related Questions