gpt4 book ai didi

iOS:将 underlineStyle 属性添加到 NSAttributedString,使 TextView 消失

转载 作者:行者123 更新时间:2023-11-28 11:35:49 27 4
gpt4 key购买 nike

我在 Storyboard中创建了一个 UITextView。

向字符串添加属性。

一切顺利,直到我添加 underlineStyle 属性。

然后 TextView 消失。

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

最佳答案

试试这个属性

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

关于iOS:将 underlineStyle 属性添加到 NSAttributedString,使 TextView 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55450482/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com