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

查看:19
本文介绍了调用 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 协议,所以你必须使用一个 NSArchiver 和一个 NSUnarchiver复制原来的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天全站免登陆