Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic Link Detection not working in NSTextView after programmatically setting text

I have an NSTextView with automatic link detection enabled. When I set the text programmatically [myTextView setString:@"http://google.com"] it doesn't automatically show the link.

If I type anything into the text view it will add the link. I want it to add the link

like image 360
Randall Avatar asked Aug 14 '11 05:08

Randall


2 Answers

Had to spent some time searching for solution, but could not find it anywhere.

You do not need any third party libraries. Cocoa will do it for you.

checkTextInDocument: works only on editable textViews (Apple forgot to mention this). Here is code which works if your NSTextView is read only:

[myTextView setEditable:YES];
[myTextView checkTextInDocument:nil];
[myTextView setEditable:NO];

Do not forget to check "Smart links" in your .xib file

like image 95
Oleg Korzhukov Avatar answered Nov 17 '22 07:11

Oleg Korzhukov


I ended up adding a category that would do the job. It relies on a couple other categories for finding and formatting links.

I wrote a blog post about it here.

I also put a sample project up on GitHub.

like image 6
Randall Avatar answered Nov 17 '22 08:11

Randall