---
title: "URL linking in a non-editable NSTextView"
date: 2013-11-09
url: "http://travisjeffery.com/url-linking-in-a-non-editable-nstextview/"
tags: ["mac"]
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://travisjeffery.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# URL linking in a non-editable NSTextView

You’re likely here because you thought you could just do this:

``` objc
NSTextView *textView = [NSTextView new];
[textView setEditable:NO];
[textView setAutomaticLinkDetectionEnabled:YES];
[textView setString:@"homesite: http://travisjeffery.com/ and twitter: http://twitter.com/travisjeffery"];
// ...
```

And you’d have clickable links in your NSTextView. Nope. setAutomaticLinkDetectionEnabled: works only when the NSTextView is editable.

The simplest solution is to build an NSAttributedString with links found using an NSDataDetector. Like so:

``` objc
[textView.textStorage setAttributedString:[self autoLinkURLs:@"homesite: http://travisjeffery.com/ and twitter: http://twitter.com/travisjeffery"]];

// ..

- (NSAttributedString *)autoLinkURLs:(NSString *)string {
    NSMutableAttributedString *linkedString = [[NSMutableAttributedString alloc] initWithString:string];

    NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    [detector enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
        if (match.URL) {
            NSDictionary *attributes = @{ NSLinkAttributeName: match.URL };
            [linkedString addAttributes:attributes range:match.range];
        }
    }];

    return [linkedString copy];
}
```

