Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView custom link color for range

I have an NSTextView for which I have set default link style attributes using the -setLinkTextAttributes: method. This is the style I wish to use for outgoing links.

In the text view I also have clickable areas that trigger functions inside the text view. I have implemented these as links. I want these to be styled independently of the outgoing links. So the logical way to write the code was like this:

[attrStr addAttribute:NSLinkAttributeName
                value:@"myapp://togglesomething"
                range:hlRange];

[attrStr addAttribute:NSForegroundColorAttributeName
                value:[NSColor yellowColor]
                range:hlRange];

But the color of the links do not change to the one i set here.

So the question is:

  1. Can I change the color of individual links?
  2. If not, can I create an area that behaves as a link without being a link item?
like image 363
Christoffer Avatar asked Jun 23 '13 15:06

Christoffer


2 Answers

If you don't explicitly set the NSForegroundColorAttributeName in setLinkTextAttributes, you can override this for individual link ranges.

i.e. just set:

[_textView setLinkTextAttributes:@{NSCursorAttributeName:[NSCursor pointingHandCursor]}];

And color your link ranges like you have above.

like image 119
Chen Lim Avatar answered Sep 21 '22 08:09

Chen Lim


Solution from Chen Lim worked.

[self.textView setLinkTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],}];

As pointed in the original question. Can we have different colors for different links. setLinkTextAttributes sets the attributes for all the links.

like image 23
KSC Avatar answered Sep 24 '22 08:09

KSC