How can I change the cursor when it's over an NSButton?
Cocoa: change cursor when it's over an NSButton
Asked Answered
All the answers mentioned here do not work when the button is above TextView. For this case refer #16288124 –
Marlinmarline
[yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];
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
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;
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
[yourButton addCursorRect:[yourButton bounds] cursor:[theCursorYouWant]];
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
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)
}
}
I recommend calling
super.resetCursorRects()
prior to calling addCursorRect(bounds, cursor: .pointingHand)
–
Brno 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)
}
}
Have the button add a cursor rect.
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.