Trigger event when hovering over a particular word in NSTextView

743 Views Asked by At

I need to create a popover when a users hovers over a particular word in NSTextView e.g

"The boy owns a dog"

Once the user has the cursor over a dog (mouse up with nothing selected), a cocoa event needs to fire so i can perform an action, which is show a popover my case

I thought NSAttributedString might be able to help, but don't see any hover events or tracking ability built in.

1

There are 1 best solutions below

0
Asperi On

The possible solution is by using NSTextAttachment and attachmentCell property.

So here is a way

  1. subclass your custom cell from NSTextAttachmentCell, so you have access to override wantsToTrackMouse, highlight... and trackMouse... interfaces

  2. create empty NSTextAttachment (if needed it can also be subclassed) and set your instance of cell to attachmentCell

  3. add above text attachment to your NSTextStorage (mutable attributed string) in desired word's range with NSAttributedString.Key.attachment