Cocoa: change cursor when it's over an NSButton
Asked Answered
A

5

18

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

Accroach answered 27/5, 2010 at 22:52 Comment(1)
All the answers mentioned here do not work when the button is above TextView. For this case refer #16288124Marlinmarline
C
17
[yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];
Christianity answered 8/2, 2011 at 18:57 Comment(1)
From Apple docs: This method is intended to be invoked only by the resetCursorRects method. If invoked in any other way, the resulting cursor rectangle will be discarded the next time the view's cursor rectangles are rebuilt. So you have to subclass NSButton.Douce
I
41

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;  
Impuissant answered 7/11, 2012 at 9:15 Comment(4)
Could you expand a little on your answer. NSButton doesn't have a cursor instance variable? I don't understand your answer.Instep
Subclassing each and every button I have in the UI just to apply a hovering cursor seem unreasonable. After telling you you shouldn't call addCursorRect directly, docs also tell you never to call "resetCursorRects" directly, and only call "invalidateCursorRects" which is an NSWindow method.Blowing
@MottiShneor Calling [super resetCursorRects] within -resetCursorRects itself doesn't violate the "never call this" rule as you're not actually invoking it when it was supposed to anyway.Cromwell
@MottiShneor Surely you'd subclass only once then use the subclass wherever you want the behaviour.Couchman
C
17
[yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];
Christianity answered 8/2, 2011 at 18:57 Comment(1)
From Apple docs: This method is intended to be invoked only by the resetCursorRects method. If invoked in any other way, the resulting cursor rectangle will be discarded the next time the view's cursor rectangles are rebuilt. So you have to subclass NSButton.Douce
U
4

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)
    }
}
Uncharitable answered 29/4, 2019 at 13:32 Comment(1)
I recommend calling super.resetCursorRects() prior to calling addCursorRect(bounds, cursor: .pointingHand)Brno
L
-1

If NSButton in macOS, swift 5:

import Cocoa 
class ViewController: NSViewController {

    @IBOutlet weak var titleBtn: NSButton!
        
     override func viewDidLoad() {
       titleBtn.addCursorRect(titleBtn.bounds, cursor: .pointingHand)
     }
}
Lindell answered 2/10, 2020 at 14:24 Comment(0)
P
-4

Have the button add a cursor rect.

Pettiford answered 27/5, 2010 at 23:11 Comment(6)
Was the down-vote necessary? A quick search of the documentation for "cursor rect" tells you plenty.Prepared
@JoshuaNozzi yes, since this is not, in and of itself, a useful answer.Endeavor
@BenLeggiero You have a pretty odd definition of "useful". Given Chuck's answer was correct but merely brief, I think a downvote is a bit heavy-handed. Other SO'ers have the option to write a better answer or upvote one. But sure, let's punish attempts at helping others.Prepared
@JoshuaNozzi This is my opinion, and it might not be shared by others on SO: To be useful, and answer should stand on its own. If it is tied to using an API, it should provide at least one example (in the answer text itself) of how to use that API. Otherwise, it should provide example code for how to do it yourself. IMHO, answers like this are just as good as posting a link to a Google search.Endeavor
@BenLeggiero A prime example of what drives knowledgable volunteers away from the site. Why participate if you're punished for "helping, just not enough for someone's taste"?Prepared
@JoshuaNozzi I'm just going by the site-wide answering guidelines, which say things like "Brevity is acceptable, but fuller explanations are better," and "try to mention any limitations, assumptions or simplifications".Endeavor

© 2022 - 2024 — McMap. All rights reserved.