iOS: добавление атрибута underlineStyle к NSAttributedString, делает текстовое представление исчезающим - PullRequest
0 голосов
/ 01 апреля 2019

Я создал UITextView в раскадровке.

Добавлены атрибуты в строку.

Все идет хорошо, пока я не добавлю атрибут underlineStyle.

Затем текстовое представление исчезнет.

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
    }
}

1 Ответ

2 голосов
/ 01 апреля 2019

Попробуйте это атрибуты

let attributes: [NSAttributedString.Key: Any] = [
    .underlineColor: underLineColor,
    .font: UIFont.systemFont(ofSize: 25),
    .underlineStyle: NSUnderlineStyle.thick.rawValue | NSUnderlineStyle.single.rawValue
]
...