Re: Cursor over links in NSTextView
Re: Cursor over links in NSTextView
- Subject: Re: Cursor over links in NSTextView
- From: Brian Webster <email@hidden>
- Date: Tue, 4 Dec 2001 12:20:42 -0600
On Tuesday, December 4, 2001, at 12:17 AM, cocoa-dev-
email@hidden wrote:
I got my code working that uses NSScanner to recognize URLs and add a
NSLinkAttribute as well as highlighting the URL, and clicking
on the URL
will launch the url as appropriate. (After cleaning up the
code, I'll be
releasing it somewhere appropriate.) But I've still got an
I-bar cursor
when the mouse moves over the link. Is there any way to tell
the system to
switch to another cursor when the mouse moves over a link?
I don't think there's anything built into NSTextView for this,
but you can do it yourself using cursor rectangles, which are
covered in the docs for NSView. There may be some sample code
out there somewhere for this, but here's a rough outline of what
would need to be done.
1. Make a subclass of NSTextView and use that to display your text
2. In your subclass, override the resetCursorRects method. This
will be called anytime your cursor rects need to be recreated,
such as when your text view is resized.
3. For each link in your text, you'll want to find the rectangle
that encloses the text for that link. One method you could use
for this is NSLayoutManager's method:
- (NSRectArray)rectArrayForCharacterRange:(NSRange)charRange
withinSelectedCharacterRange:(NSRange)selCharRange
inTextContainer:(NSTextContainer *)aTextContainer
rectCount:(unsigned *)rectCount
That's a mouthful, huh? The charRange argument should be the
range of characters that makes up the link, and selCharRange can
be the same thing. The text container object you pass in can
just be [myTextView textContainer], where myTextView is your
text view. The layout manager object to send this message to
can be accessed by [myTextView layoutManager]. Notice that this
method returns an array of rectangles, since the range of
characters could potentially go over a line break. After you
get the rectangle(s), just use NSView's addCursorRect:cursor:
method to add the cursor rectangle.
I hope this is helpful. It looks a little daunting, but I don't
think it'll really be too bad to implement.
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster