<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>farp.blog &#187; NSTextView</title>
	<atom:link href="http://fredandrandall.com/blog/tag/nstextview/feed/" rel="self" type="application/rss+xml" />
	<link>http://fredandrandall.com/blog</link>
	<description>Bloggin&#039; about whatever</description>
	<lastBuildDate>Tue, 04 Nov 2014 07:15:30 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Automatic Link Detection in an NSTextView</title>
		<link>http://fredandrandall.com/blog/2011/08/16/automatic-link-detection-in-an-nstextview/</link>
		<comments>http://fredandrandall.com/blog/2011/08/16/automatic-link-detection-in-an-nstextview/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 03:48:19 +0000</pubDate>
		<dc:creator><![CDATA[Randall]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[NSTextView]]></category>
		<category><![CDATA[objc]]></category>

		<guid isPermaLink="false">http://fredandrandall.com/blog/?p=506</guid>
		<description><![CDATA[NSTextView is a really powerful text view class. It can do all kinds of stuff like automatic spelling correction, email, phone number, and address detection, and link detection. I was working on a chat application and I wanted to have &#8230; <a href="http://fredandrandall.com/blog/2011/08/16/automatic-link-detection-in-an-nstextview/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>NSTextView is a really powerful text view class. It can do all kinds of stuff like automatic spelling correction, email, phone number, and address detection, and link detection. I was working on a chat application and I wanted to have links that people sent be clickable. I was using an NSTextView because it has the automaticLinkDetection property.</p>
<p>Unfortunately, I couldn&#8217;t get links to show up in my app. I tried sending it delegate messages and lots of other stuff. It just wouldn&#8217;t work when I set the text programatically. It would only work when I was actually typing in to the NSTextView and even then, it was a little flaky. I posted a <a href="http://stackoverflow.com/questions/7055131/automatic-link-detection-not-working-in-nstextview-after-programmatically-setting">question on StackOverflow</a> and didn&#8217;t get any answers.</p>
<p>So to fix the problem, I did the link detection myself. I look through the text view for links and then add them myself as NSAttributedStrings. I did this using three different categories.</p>
<p>There is <a href="http://snippets.aktagon.com/snippets/358-How-to-make-a-clickable-link-inside-a-NSTextField-and-Cocoa">NSAttributedString+Hyperlink</a>, NSString+FindURLs, and NSTextView+AutomaticLinkDetection. I use the FindURLs category to find the links and their locations, then I use the Hyperlink category to format the string like a link, and the AutomaticLinkDetection category to bring them together.</p>
<p>You use it like this</p>
<pre class="brush: objc; title: ; notranslate">
   [myTextView setString:@&quot;http://google.com http://apple.com&quot;];
   [myTextView detectAndAddLinks];
</pre>
<p>And here is the implementation</p>
<pre class="brush: objc; title: ; notranslate">
-(void)detectAndAddLinks
{
   NSArray *linkLocations = [[self string] locationsOfLinks];
   NSArray *links = [[self string] arrayOfLinks];

   int i=0;
   for( NSString *link in links )
   {
      NSAttributedString *linkString = [NSAttributedString hyperlinkFromString:link withURL:[NSURL URLWithString:link]];
      [[self textStorage] replaceCharactersInRange:[[linkLocations objectAtIndex:i] range] withAttributedString:linkString];
      i++;
   }

}
</pre>
<p>I put an <a href="https://github.com/blladnar/AutoLink">example project on GitHub</a> that you can look at and use.</p>
<p>Let me know if there was something I missed that would make it so I don&#8217;t have to use the category.</p>
]]></content:encoded>
			<wfw:commentRss>http://fredandrandall.com/blog/2011/08/16/automatic-link-detection-in-an-nstextview/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
