Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa osx NSButton show text when mouse over

In my application for Mac I want to show some info text when the users moves the mouse pointer over a button. Something like this:

enter image description here

How can I achieve this correctly?

Thanks in advance.

like image 654
user3065901 Avatar asked Apr 29 '15 16:04

user3065901


2 Answers

This works for me in Xcode 6.2:

In the Identity Inspector(the pane on the right hand side in the image below), in the Tool Tip section enter "Sad face":

enter image description here

like image 93
7stud Avatar answered Oct 26 '22 02:10

7stud


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

var trackingArea: NSTrackingArea!

Add a tracking area for the view

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

Mouse entered Event

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

Or you can make a separate tooltip for each range of a string: https://stackoverflow.com/a/18814112/308315

like image 32
Barath Avatar answered Oct 26 '22 01:10

Barath