To implement trackingArea in MacOS, I did it that way:
Subclass UILabel to handle the mouse events
class LabelWithHelp: NSTextField {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here. If needed
}
// MARK: - Mouse Events
// --------------------- mouseEntered --------------------------------------------------------------
// Description: When hovering over label
// Parameters
// theEvent: NSEvent
// Comments :
// Need to have defined trackingArea
// -------------------------------------------------------------------------------------------------
override func mouseEntered(with theEvent: NSEvent) {
super.mouseEntered(with: theEvent)
print("Entered")
}
// --------------------- mouseExited --------------------------------------------------------------
// Description: Exit from hover
// Parameters
// theEvent: NSEvent
// Comments :
// -------------------------------------------------------------------------------------------------
override func mouseExited(with theEvent: NSEvent) {
super.mouseExited(with: theEvent)
print("Exited")
arrowCursor!.set()
}
// --------------------- cursorUpdate --------------------------------------------------------------
// Description: set cursor as pointing hand
// Parameters
// event: NSEvent
// Comments :
// -------------------------------------------------------------------------------------------------
override func cursorUpdate(with event: NSEvent) {
pointingHandCursor!.set()
}
}
Declare for each object (here label) for which you want hovering (declare the right class in IB)
@IBOutlet var weak myLabel: LabelWithHelp!
fileprivate var labelTrackingArea : NSTrackingArea?
In windowDidLoad
if labelTrackingArea == nil { // Avoid creating multiple times
labelTrackingArea = NSTrackingArea(rect: myLabel.bounds, options: [NSTrackingArea.Options.mouseEnteredAndExited, NSTrackingArea.Options.cursorUpdate, NSTrackingArea.Options.activeInKeyWindow], owner: myLabel, userInfo: nil)
myLabel.addTrackingArea(labelTrackingArea!)
}
The options allow for tracking mouse in, mouse out and change cursor if needed
NOTE: there is also a very simple way: use tooltips
@IBOutlet var weak myLabel: UILabel!
myLabel.toolTip = "Hover over label"
You have not as much control, but so simple.
You can even declare it directly in IB, in Identity inspector