简体   繁体   中英

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. I don't want the link text to be the default blue color so I changed it to orange with .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). Here is the link: Change the colour of the bullets in UITextView

Use linkTextAttributes with a UITextView .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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