skip to Main Content

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
    }

}

2

Answers


  1. 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

    Login or Signup to reply.
  2. Use linkTextAttributes with a UITextView.

    textView.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.yellow]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search