Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the cursor to a pointing hand over a text view

Is there a way to set the cursor to a pointing hand over a text view without subclassing NSTextView?

I read a lot about NSTrackingAreas, tested a lot of examples, set different tracking options and implemented different methods, but the cursor still remains an I-Beam. I have read that it is an AppKit automatic feature, so how can I prevent this?

Thank you!

like image 960
berfis Avatar asked Dec 01 '12 10:12

berfis


People also ask

How do I make a cursor hand in HTML?

Use CSS property to create cursor to hand when user hovers over the list of items. First create list of items using HTML <ul> and <li> tag and then use CSS property :hover to cursor:grab; to make cursor to hand hover the list of items.

What is the hand cursor called?

It is also called a pointer, but today pointer refer to a specific cursor, the one that looks like a hand with an extended index finger.

How do I change cursor to pointer in CSS?

You can simply use the CSS cursor property with the value pointer to change the cursor into a hand pointer while hover over any element and not just hyperlink. In the following example when you place the cursor over the list item, it will change into a hand pointer instead of the default text selection cursor.


2 Answers

I had to subclass. After a couple of hours to test a lot of methods and options, this finally worked :

@implementation ATTextView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _trackingArea = [[NSTrackingArea alloc]initWithRect:[self bounds] options: (NSTrackingMouseMoved | NSTrackingActiveInKeyWindow) owner:self userInfo:nil];
        [self addTrackingArea:_trackingArea];
    }
    return self;
}

- (void)mouseMoved:(NSEvent *)event
{
    if ([self isEditable]) [[NSCursor IBeamCursor] set];
    else [[NSCursor pointingHandCursor] set];
}

- (void)updateTrackingAreas {
    [super updateTrackingAreas];
    [self removeTrackingArea:_trackingArea];
    _trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options: (NSTrackingMouseMoved | NSTrackingActiveInKeyWindow) owner:self userInfo:nil];
    [self addTrackingArea:_trackingArea];
}
@end

Just to find the correct example (Cocoa is 80% doc reading and 20% coding): https://developer.apple.com/library/mac … 0i-CH8-SW1

like image 160
berfis Avatar answered Jan 01 '23 11:01

berfis


this worked for me using Swift 3:

class HyperlinkTextField : NSTextField {

    override func resetCursorRects() {
        discardCursorRects()
        addCursorRect(self.bounds, cursor: NSCursor.pointingHand())
    }

}
like image 42
David Green Avatar answered Jan 01 '23 11:01

David Green