Cocoa osx NSButton displays text when mouse

In my Mac app, I want to show some informational text when users move their mouse over a button. Something like that:

enter image description here

How can I achieve this correctly?

Thanks in advance.


source to share

4 answers

This works for me in Xcode 6.2:

In the Identity Inspector (panel on the right in the image below), in the section, Tool Tip

enter "Sad face":

enter image description here



In Interface-Builder, you can set a "hint" for most objects, including the NSButton (Open Inspector, then select the Help topic). However, if you are using NSToolbar, it also has tooltips; you can do it programmatically. Try typing setToolTip in your source, then select an option - double click it for more information. (Option = alternative).



To programmatically add a custom tooltip in Swift, subclass the appropriate view

var trackingArea: NSTrackingArea!


Add a tracking area to view

let opts: NSTrackingAreaOptions = ([NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways])
trackingArea = NSTrackingArea(rect: bounds, options: opts, owner: self, userInfo: nil)


Injected mouse event

override func mouseEntered(theEvent: NSEvent) {
    self.tooltip = "Sad face : Select the option for very poor"


Or you can make a separate hint for each range of the string: / ...



You can also do this programmatically.

(Let's assume someButton is your NSButton object)

[someButton setToolTip:@"Sad face: Select this option for \"Very poor\""];




All Articles