Home > Back-end >  How to change html list bullet color for a link item in swift?
How to change html list bullet color for a link item in swift?

Time:01-27

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
    }

}

CodePudding user response:

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

CodePudding user response:

Use linkTextAttributes with a UITextView.

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