5
votes

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?
2

2 Answers

4
votes

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.

3
votes

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.