[英]How to change html list bullet color for a link item in swift?
我有UITextView
,為此我使用 HTML 字符串,其中包含兩個元素的列表,其中一個是鏈接。 我不希望鏈接文本成為默認的藍色,因此我使用.linkTextAttributes
將其更改為橙色。 但是如何為列表項目符號設置不同的顏色呢? 它仍然是藍色的
class ViewController: UIViewController {
var textView = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
let htmlString = """
<p>My list</p>
<ul>
<li><a>Item</a></li>
<li><a href=\"https://www.apple.com">Link item</a></li>
</ul>
"""
textView.linkTextAttributes = [ .foregroundColor: UIColor.orange ]
let modifiedFont = String(format:"<span style=\"font-family: \(String(describing: UIFont.italicSystemFont)); font-size: \(45)\">%@</span>", htmlString)
guard let htmlData = modifiedFont.data(using: String.Encoding.utf8, allowLossyConversion: false) else {
return
}
let attributedString = try! NSAttributedString(data: htmlData,
options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html,
NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
self.textView.attributedText = attributedString
}
}
我不知道 Swift 但我只是 gg 並發現了與您類似的問題(在 2015 年提出)。 這是鏈接: 更改 UITextView 中項目符號的顏色
將linkTextAttributes
與UITextView
一起使用。
textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.yellow]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.