问题
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?
回答1:
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 because NSTextField
doesn't conform to the NSCopying
protocol, so you have to use an NSArchiver
and an NSUnarchiver
to duplicate the original NSTextField
, and even then some attributes are not copied, such as constraints.
The easy way is to hide and un-hide the NSTextField
.
NSTextField *textField;
{...}
[textField setStringValue:newText];
[textField setHidden:YES];
[textField setHidden:NO];
This makes AppKit call expansionFrameWithFrame:inView:
on the NSTextField
's NSTextFieldCell
, which properly updates the expansion tooltip's presence and size.
回答2:
[textField resetCursorRects]
seems to cause expansionFrameWithFrame:inView:
to be called on the NSTextFieldCell
as well. Tested only in macOS 10.14.
来源:https://stackoverflow.com/questions/21920982/how-do-i-update-the-expansion-tooltip-size-after-calling-nstextfield-setstringva