1

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.

Max Desiatov
  • 5,087
  • 3
  • 48
  • 56
Jimmy
  • 1,433
  • 2
  • 14
  • 19
  • How could the cursor be over a word longer than one letter? – El Tomato Jul 01 '20 at 10:49
  • If you are talking about a selection of text, take a look at the `NSTextView`'s `textViewDidChangeSelection` notification method. By the way, tigger is not an English word. – El Tomato Jul 01 '20 at 10:55
  • Not sure how ```textViewDidChangeSelection``` helps here, as the text has not changed ? – Jimmy Jul 01 '20 at 11:04
  • > How could the cursor be over a word longer than one letter? Sorry, don't understand this comment. Sorry, don't understand this comment. – Jimmy Jul 01 '20 at 11:05
  • The cursor appears before or after a letter, not over two or more letters. – El Tomato Jul 01 '20 at 11:07
  • Don't blame me for your down vote. It's not me. – El Tomato Jul 01 '20 at 11:11
  • Np El Tomato, I appreciate you are just looking for clarification. – Jimmy Jul 01 '20 at 11:20
  • 1
    Yes, this is possible. You need to get the mouse location from `mouseMoved`, then use the `layoutManager` of the `textView` to find the word. I am not near a Mac, so cannot get you an answer. But hopefully these keywords will get you on your way. See my question about something similar here: https://stackoverflow.com/questions/54610163/nslayoutmanager-returns-incorrect-glyphindex-for-mouseposition-in-nstextview – koen Jul 01 '20 at 11:29
  • All you have to do is to use `NSTextView`'s `selectedRange()` along with its `textViewDidChangeSelection` notification method. – El Tomato Jul 01 '20 at 14:15
  • @Jimmy: when you say hover, do you mean with the mouse up (no selection) or down (selection) ? – koen Jul 01 '20 at 18:50
  • Hi @koen, mouse up (no selection) – Jimmy Jul 01 '20 at 20:04
  • 1
    Ok, so see my suggestion above. And please make that clear in your question. – koen Jul 01 '20 at 22:36
  • @koen awesome, your suggestion worked. Do you want to answer and I can accept ? – Jimmy Jul 02 '20 at 18:48
  • 1
    @Jimmy Glad it's working. It's perfectly fine if you post the answer yourself, I only pointed you in the right direction. If it was helpful, feel free to up-vote my Q/A in the link I provided. – koen Jul 02 '20 at 18:53

1 Answers1

3

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

Asperi
  • 228,894
  • 20
  • 464
  • 690