0

I would like to style each link differently within either a UITextView or UILabel. I am currently using UITextViews with links but I want URLs to appear purple and usernames to appear black. I've also looked into TTTAttributedLabel but cannot seem to find out how to accomplish this.

textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
attributedString.setAttributes([NSLinkAttributeName: url, range: range)
attributedString.addAttributes([NSForegroundColorAttributeName: UIColor.purple], range: range)

In the example above, which is what I have tried, the link will be orange, not purple. If I do not set linkTextAttributes, links will be blue. How do I override linkTextAttributes with different attributes per link?

Josh
  • 103
  • 1
  • 6
  • Here this link can help you: [http://stackoverflow.com/questions/28361072/change-the-color-of-a-link-in-an-nsmutableattributedstring](http://stackoverflow.com/questions/28361072/change-the-color-of-a-link-in-an-nsmutableattributedstring) [http://stackoverflow.com/questions/13405513/change-uitextview-hyperlink-color/21027340#21027340](http://stackoverflow.com/questions/13405513/change-uitextview-hyperlink-color/21027340#21027340) – Renato Ioshida Nov 18 '16 at 17:40
  • Thank you, but I have already tried linkTextAttributes. My question is about different attributes per link. In fact, my question was asked in a comment in your first link, but no answer was given http://stackoverflow.com/questions/28361072/change-the-color-of-a-link-in-an-nsmutableattributedstring#comment62635278_32222702 @RenatoIoshida – Josh Nov 20 '16 at 05:23

1 Answers1

0

linkTextAttributes can't designate the text behavior for each word. Have you try this? https://github.com/rinov/RegeributedTextView

and

textView.addAttribute("text1", attribute: .linkColor(.orange))
textView.addAttribute("text2", attribute: .linkColor(.purple))

NSRange is automatically calculated.

Pang
  • 9,564
  • 146
  • 81
  • 122
rinov
  • 1
  • 1