NSTextView custom link color for range

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

In the text view, I also have interactive access areas that call functions inside the text view. I implemented them as links. I want them to be styled independently of outbound links. The logical way to write the code was as follows:

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

But the color of the links does not change to the one I set here.

So the question is:

  • Can I change the color of individual links?
  • If not, can I create an area that behaves like a link without being an element of the link?
+4
source share
2 answers

Unless you explicitly specify NSForegroundColorAttributeName in setLinkTextAttributes, you can override this for individual reference ranges.

i.e. just install:

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

And color the link ranges as above.

+3
source

The solution from Chen Lim worked.

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

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

+3
source

Source: https://habr.com/ru/post/1487733/


All Articles