I've create a UITextView in storyboard.
Added attributes to the string.
All goes well, until I add underlineStyle attribute.
Then the text view disappears.
class ViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let text = "random text <a href='http://www.google.com'>http://www.google.com </a> more random text"
let attributedText = htmlStyleAttributeText(text: text)!
let underLineColor: UIColor = UIColor(red: 245/255, green: 190/255, blue: 166/255, alpha: 1)
let attributes: [NSAttributedString.Key: Any] =
[.underlineColor: underLineColor,
.font: UIFont.systemFont(ofSize: 25),
.underlineStyle: NSUnderlineStyle.thick] // Adding this makes the UITextView disappear.
let wholeRange = NSRange(attributedText.string.startIndex..., in: attributedText.string)
attributedText.addAttributes(attributes, range: wholeRange)
textView.attributedText = attributedText
}
public func htmlStyleAttributeText(text: String) -> NSMutableAttributedString? {
if let htmlData = text.data(using: .utf8) {
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue]
let attributedString = try? NSMutableAttributedString(data: htmlData, options: options, documentAttributes: nil)
return attributedString
}
return nil
}
}