How do I update the expansion tooltip size after calling NSTextField setStringValue:?

孤者浪人 提交于 2019-12-06 13:25:31

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.

[textField resetCursorRects] seems to cause expansionFrameWithFrame:inView: to be called on the NSTextFieldCell as well. Tested only in macOS 10.14.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!