Re: Changing cursor when mouse over a link
Re: Changing cursor when mouse over a link
- Subject: Re: Changing cursor when mouse over a link
- From: j o a r <email@hidden>
- Date: Fri, 28 Dec 2001 22:27:40 +0100
On Friday, December 28, 2001, at 08:32 , Steven Frank wrote:
So, I was just reading about how to make clickable links in an
NSTextView:
http://www.cocoadev.com/index.pl?ClickableUrlInTextView
But does anybody know what has to be done to make the "hand with
pointing finger" cursor appear when you mouse over the link, like in
Mail?
I searched the archives for cocoa-dev and macosx-dev and saw a similar
question asked back in October, but I didn't see any answers.
I did something like this in a subclass of NSTextView used to display
HTML with links. It probably has room for improvements, but works pretty
fine (read disclaimer at the end of this email).
Three things:
First in "init" (I snatched the image for the cursor somewhere in the
application wrapper of some other app, can't remember which one):
//************************************************************************
// Add the cursor for links
NSImage *cursorImage = [NSImage imageNamed:@"LinkCursor"];
linkCursor = [[NSCursor alloc] initWithImage:cursorImage
hotSpot:NSMakePoint(0.0,0.0)];
//************************************************************************
Then parsing the string for links:
//************************************************************************
- (void)setCursorRectsForLinks
{
NSTextStorage *attrString = [self textStorage];
unsigned loc = 0;
unsigned end = [attrString length];
while (loc < end) {
NSRange linkRange;
id tmp = [attrString attribute:NSLinkAttributeName atIndex:loc
longestEffectiveRange:&linkRange inRange:NSMakeRange(loc, end-loc)];
if (tmp != nil) {
NSRect linkRect = [[self layoutManager]
boundingRectForGlyphRange:linkRange inTextContainer:[self
textContainer]];
[self addCursorRect:linkRect cursor:linkCursor];
loc = NSMaxRange(linkRange);
} else {
loc++;
}
}
}
//************************************************************************
Keeping track of updates to the layout (called automatically when the
layout changes):
//************************************************************************
- (void)resetCursorRects
{
[super resetCursorRects];
[self setCursorRectsForLinks];
}
//************************************************************************
You could also search for my mail to this list about a chaveat about
HTML containing tables not working.
Regards,
j o a r