Tootips in NSTextView/NSTextfields


Sak Wathanasin
 

I want to display tooltips over parts of an attributed string, and thought that using an
NSToolTipAttributeName attribute on the required ranges would do the trick but I can't get it to
work. In my toy (macOS) app, I have a NSTxtView and in my aWakeFromNib, I do

let testString = "Test text with tooltip"
let testNSString = testString as NSString
let tooltipRange = testNSString.range(of: "tooltip")
self.textView.textStorage?.replaceCharacters(in: NSMakeRange(0,
self.textView.textStorage!.length), with: testString)
self.textView.textStorage?.setAttributes([NSAttributedStringKey.toolTip: "sample tooltip
text" as NSString,
NSAttributedStringKey.foregroundColor:
NSColor.red], range: tooltipRange)

The word "tooltip" is shown in red as expected, but no tooltip appears when I hover over it. What
am I missing?

Thanks for any help,

Sak


Andy Lee
 

On Jan 4, 2018, at 9:06 AM, Sak Wathanasin <sw@...> wrote:
The word "tooltip" is shown in red as expected, but no tooltip appears when I hover over it. What
am I missing?
Your code works for me. It's a little fussy though about placement of the text cursor. I had to position the tiny horizontal mark in the middle of the text cursor just *under* the word "tooltip" instead of directly over it.

--Andy


Andy Lee
 

On Jan 4, 2018, at 12:25 PM, Andy Lee <aglee@...> wrote:
Your code works for me.  It's a little fussy though about placement of the text cursor.  I had to position the tiny horizontal mark in the middle of the text cursor just *under* the word "tooltip" instead of directly over it.

I did find a problem: the tooltip doesn't seem to work the first time I position the cursor, only on subsequent attempts.

For testing purposes I found it helped to add .cursor:NSCursor.pointingHand to the attribute dictionary.  That cursor's hot spot is the tip of the finger, so to me it feels easier to position the cursor in a way that triggers the tooltip.

I wonder if there is a bug in the iBeam cursor.  The docs say the default hot spot is "where the crossbeam intersects the I", but it doesn't seem to work that way for purposes of triggering the tooltip.

--Andy


Sak Wathanasin
 


On 04 Jan 2018, at 18:00, Andy Lee <aglee@...> wrote:

I did find a problem: the tooltip doesn't seem to work the first time I position the cursor, only on subsequent attempts.

For testing purposes I found it helped to add .cursor:NSCursor.pointingHand to the attribute dictionary.  That cursor's hot spot is the tip of the finger, so to me it feels easier to position the cursor in a way that triggers the tooltip.


Thank you, thank you, thank you - I had just about resigned myself to having to subclass NSTextView and tracking the mouse etc. The issue with its not showing the tooltip the first time is a bit weird; maybe it has something to do with first responders - I'll play around with it.

Thanks again,
Sak