NSTextView has a nice instance method: setAutomaticLinkDetectionEnabled:. When this is set to YES, the textview turns any properly formatted URL into a hyperlink in blue text with a single underline and the pointing hand cursor. It couldn’t be more straightforward.
Query uses this extensively in its result views. With a little tweak here and there, it plays well with syntax coloring as well:
![]()
While it serves the purpose, it doesn’t feel quite alright. If you answer yes to any of the following:
- Is your primary browser Safari?
- Do you use content readers such as NetNewsWire, newsfire, NewsLife, etc.?
- Do you use site specific browsers such as Fluid, Gmail Browser, Mailplane, etc.?
you’re likely to find the default link cursor awkward, ever so slightly, because the same link will look like this in your favorite applications:
![]()
It turns out that WebKit uses its own custom image for the pointing hand cursor: /System/Library/Frameworks/WebKit.framework/WebCore.framework/Resources/linkCursor.png
If you are anything like me, you, too, can override the link cursor in your NSTextView subclass with setLinkTextAttributes: instance method.
Here’s a quick how-to:
- Copy the custom cursor image into your project’s Resources folder.
- In the
awakeFromNibmethod of your controller class, set this custom image as the link cursor:
- (void)awakeFromNib { /* do standard awakeFromNib stuff */ // prepare attributes NSCursor *customLinkCursor = [[NSCursor alloc] initWithImage:[NSImage imageNamed:@"linkCursor.png"] withHotSpot:NSMakePoint(0, 0)]; NSDictionary *customLinkTextAttributes = [NSDictionary dictionaryWithObject:customLinkCursor forKey:NSCursorAttributeName]; [cutomLinkCursor release]; customLinkCursor = nil; // myCustomTextView is an IBOutlet or an instance variable [myCustomTextView setAutomaticLinkDetectionEnabled:YES]; [myCustomTextView setLinkTextAttributes:customLinkTextAttributes]; }
Instead of copying the image file into your project, you could link to the local image in WebCore.framework. In my opinion, however, it would be a bit too much reliance on an unpublished feature.