简体   繁体   English

如何更改 swift 中链接项目的 html 列表项目符号颜色?

[英]How to change html list bullet color for a link item in swift?

I have UITextView and for it I use an HTML string that contains a list of two elements, one of which is a link.我有UITextView ,为此我使用 HTML 字符串,其中包含两个元素的列表,其中一个是链接。 I don't want the link text to be the default blue color so I changed it to orange with .linkTextAttributes .我不希望链接文本成为默认的蓝色,因此我使用.linkTextAttributes将其更改为橙色。 But how to set a different color for the list bullet?但是如何为列表项目符号设置不同的颜色呢? It is still blue它仍然是蓝色的

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
    }

}

I don't know Swift but I just gg and found similar question to yours (asked in 2015).我不知道 Swift 但我只是 gg 并发现了与您类似的问题(在 2015 年提出)。 Here is the link: Change the colour of the bullets in UITextView这是链接: 更改 UITextView 中项目符号的颜色

Use linkTextAttributes with a UITextView .linkTextAttributesUITextView一起使用。

textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.yellow]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM