Posts Tagged: webkit


15
Oct 09

What’s in a cursor? Part I

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: NSTextView link cursor

While it serves the purpose, it doesn’t feel quite alright. If you answer yes to any of the following:

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: WebKit link cursor

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:

  1. Copy the custom cursor image into your project’s Resources folder.
  2. In the awakeFromNib method 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.