When a view contains an NSTextField
with the expansion tooltip enabled and the text doesn't fit, then the user hovers the cursor over the field, OS X shows an expansion tooltip. If you then call setStringValue:
to change the text content of the NSTextField
, the expansion tooltip's size is not updated. For instance, if the original text was 100 characters long but the new text is only 50 characters long, hovering over the new text will show an expansion tooltip large enough for 100 characters containing the new text. This is true even if the new string fits entirely in the NSTextField
, which normally would prevent an expansion tooltip appearing.
The opposite also occurs: If the original string fits within the NSTextField
, no expansion tooltip appears. If the new string does not fit within the NSTextField
, no expansion tooltip appears even though it should.
Internally, the NSTextField
's NSTextFieldCell
implements the NSCell
method expansionFrameWithFrame:inView:
. Something (I'm not sure what) calls this once, and seems to cache the result. Setting a new string using setStringValue:
does not cause this function to be called again.
Calling setNeedsDisplay
on the NSTextField after calling setStringValue:
does not fix this.
So how do I get AppKit to resize the expansion tooltip?
After a great deal of experimentation, I found two methods to fix this.
The very difficult way is to delete and recreate the entire
NSTextField
each time the text changes. This is laborious becauseNSTextField
doesn't conform to theNSCopying
protocol, so you have to use anNSArchiver
and anNSUnarchiver
to duplicate the originalNSTextField
, and even then some attributes are not copied, such as constraints.The easy way is to hide and un-hide the
NSTextField
.This makes AppKit call
expansionFrameWithFrame:inView:
on theNSTextField
'sNSTextFieldCell
, which properly updates the expansion tooltip's presence and size.