0

I'm trying to create a generalized function/class that builds embedded hyperlinks. I am using an NSAttributedString with NSAttributedString.Key.link attribute and assigning a URL to the specified range. I can even specify multiple links within the body of text, with specific styles for the text. However the only caveat that I am seeing is that the highlighted state of the links have a grey background color. Is there a way to get rid of this background color, and on top of that assign a specified highlight state color for the link? I am open to other means of creating the text and links, but I feel like NSAttributedString is the right means.

Again:

  • the body of text should be able to handle any number of links
  • each link can contain any number of words
  • the highlighted state shouldn't have a background color of grey, and be able to accept a specified color for a highlight state
  • the text should be able to wrap if needed
  • Where are these used? A label, textfield, or textview? – rmaddy Apr 21 '19 at 18:05
  • I'm currently using a `UITextView`, but if there is a better medium to meet those requirements, then I'm open to it. I guess another requirement would be that the text should be able to wrap. I will update the list – programming_tiger Apr 21 '19 at 18:06
  • Does this help? https://stackoverflow.com/a/46551112/341994 The trick is that you must set the text view's `linkTextAttributes` to an empty dictionary. Now you can set your individual link attributes in the attributed string. – matt Apr 21 '19 at 18:09
  • But if "be able to accept a specified color for a highlight state" is key, then use a web view with your own CSS. – matt Apr 21 '19 at 18:12
  • Don't think that link helps @matt. Need to change the hyperlink color states. I believe that linkTextAttributes affect the rest of the text other than the hyperlink. Additionally I think that using a webview opens up a whole new can of worms doesn't it? – programming_tiger Apr 21 '19 at 18:23
  • Well, it opens the same can of worms you already opened. There is no magic way to tell an attributed string what the link attributes should be _after_ being clicked on (or _while_ being clicked on or whatever you're after). That's web view stuff. – matt Apr 21 '19 at 18:28
  • hmm yeah I mean it's not a desirable thing to do, but I need to build according to specs. Ideally it should be designed differently and I do agree this is a web design/paradigm. But wanted to see if something like this was achievable using native components. – programming_tiger Apr 21 '19 at 18:30
  • @rm4g3d0n The easiest way would be creating your links using html and converting the html to NSAttributedString. There are four links states, just create a style color for each as needed: a:link - unvisited link a:visited - link visited a:hover - mouses over (not available in iOS) a:active - when tapped – Leo Dabus Apr 21 '19 at 23:49

0 Answers0