调用NSTextField setStringValue:后,如何更新扩展工具提示的大小? [英] How do I update the expansion tooltip size after calling NSTextField setStringValue:?

查看:146
本文介绍了调用NSTextField setStringValue:后,如何更新扩展工具提示的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当视图包含启用扩展工具提示的NSTextField且文本不适合时,用户将光标悬停在字段上,OS X将显示扩展工具提示.如果然后调用setStringValue:来更改NSTextField的文本内容,则扩展工具提示的大小不会更新.例如,如果原始文本的长度为100个字符,而新文本的长度仅为50个字符,则将鼠标悬停在新文本上将显示一个扩展工具提示,其大小足以容纳包含新文本的100个字符.即使新字符串完全适合NSTextField,也是如此,这通常会阻止出现扩展工具提示.

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.

反之亦然:如果原始字符串适合NSTextField,则不会出现扩展工具提示.如果新字符串不适合NSTextField,则即使出现扩展提示也不会出现.

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.

在内部,NSTextFieldNSTextFieldCell实现NSCell方法expansionFrameWithFrame:inView:.某个东西(我不确定是什么)调用了一次,似乎缓存了结果.使用setStringValue:设置新字符串不会导致再次调用此函数.

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.

在调用setStringValue:之后在NSTextField上调用setNeedsDisplay不能解决此问题.

Calling setNeedsDisplay on the NSTextField after calling setStringValue: does not fix this.

那么我如何获得AppKit来调整扩展工具提示的大小?

So how do I get AppKit to resize the expansion tooltip?

推荐答案

经过大量的实验,我发现了两种解决方法.

After a great deal of experimentation, I found two methods to fix this.

非常困难的方法是每次文本更改时删除并重新创建整个NSTextField.这很麻烦,因为NSTextField不符合NSCopying协议,因此您必须使用NSArchiverNSUnarchiver复制原始的NSTextField,即使这样,某些属性也不会被复制,例如作为约束.

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.

简单的方法是隐藏和取消隐藏NSTextField.

The easy way is to hide and un-hide the NSTextField.

NSTextField *textField;
{...}
[textField setStringValue:newText];
[textField setHidden:YES];
[textField setHidden:NO];

这使AppKit在NSTextFieldNSTextFieldCell上调用expansionFrameWithFrame:inView:,从而正确更新了扩展工具提示的存在和大小.

This makes AppKit call expansionFrameWithFrame:inView: on the NSTextField's NSTextFieldCell, which properly updates the expansion tooltip's presence and size.

这篇关于调用NSTextField setStringValue:后,如何更新扩展工具提示的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆