Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: change cursor when it's over an NSButton

How can I change the cursor when it's over an NSButton?

like image 691
Matt S. Avatar asked May 27 '10 22:05

Matt S.


3 Answers

You should subclass NSButton first, then add the code below.

- (void)resetCursorRects
{
    if (self.cursor) {
        [self addCursorRect:[self bounds] cursor: self.cursor];
    } else {
        [super resetCursorRects];
    }
}

Now you can set cursor as you like.

[self.button setCursor:[NSCursor pointingHandCursor]];

Note: add cursor as a property of your subclass, like this:

@property (strong) NSCursor *cursor;  
like image 177
6 1 Avatar answered Oct 19 '22 18:10

6 1


[yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];
like image 36
maschall Avatar answered Oct 19 '22 19:10

maschall


Following the already given example, creating a subclass of NSButton in Swift 5:

import AppKit

class Button: NSButton {
    override func resetCursorRects() {
        addCursorRect(bounds, cursor: .pointingHand)
    }
}
like image 24
vauxhall Avatar answered Oct 19 '22 20:10

vauxhall