像Safari浏览器的地址栏中自动建议的NSTextField? [英] NSTextField with auto-suggestions like Safari's address bar?
问题描述
什么是有一个的NSTextField
以推荐列表动态显示下方为用户类型最简单的方法?就像有一些各种各样的菜单Safari浏览器的地址栏(我pretty信心Safari浏览器的地址栏的建议是菜单,因为它有圆角,蓝色渐变选择和背景模糊)。
What's the easiest way to have an NSTextField
with a "recommendation list" dynamically shown below it as the user types? Just like Safari's address bar that has a menu of some sorts (I'm pretty confident Safari's address bar suggestions is menu since it has rounded corners, blue gradient selection, and background blurring).
我试过使用<一个href=\"http://stackoverflow.com/questions/6725359/nstextfield-with-suggestions-drop-down\"><$c$c>NSTextView$c$c>'s自动完成设施,但发现它是不充分的:
I've tried using NSTextView
's autocompletion facility but found it was inadequate:
- 它试图完成的字的,而不是整个文本字段 - 换句话说,选择一个自动完成建议只会取代目前的字
- 轻推它自动完成列表中前进,并与插入点而不是保持它与文本字段对齐对齐。
- It tries to complete words instead of the whole text fields – in other words, selecting an autocomplete suggestion will only replace the current word.
- It nudges the autocompletion list forward and align it with the insertion point instead of keeping it align with the text field.
在每当我选择了自动完成建议的文本字段只替换与列表中所建议的项目,这将导致
。 K
上面的示例截图阿巴迪阿巴迪Kurniawan
In the sample screenshot above whenever I selected the autocomplete suggestion the text field only replaces K
with the suggested item in the list, which results in Abadi Abadi Kurniawan
.
这是想什么,我来实现的:
These are what I'd like to achieve:
- 只要选择一个建议,整个文本字段被替换的建议。
- 保持与文本字段的左边对齐建议列表。
注:这是的不的有关问题将进度指示器的背后文本字段。
推荐答案
这只是地址一半的答案,但我相信你需要继承 NSTextView
和实施 - (NSRange)rangeForUserCompletion
方法,返回整个字符串的范围在文本字段中。这应该确保它不只是自动完成最近输入的字。
This only addresses half of your answer, but I believe you need to subclass NSTextView
and implement the - (NSRange)rangeForUserCompletion
method, returning the range of the entire string in the text field. This should make sure that it doesn't just autocomplete the most recently entered word.
如果你想有一个自定义菜单,你将不得不自己做,可能是通过实施 -controlTextDidChange:
法和表显示自定义视图在适当的时候。
If you want a custom menu, you're going to have to do that yourself, probably by implementing the -controlTextDidChange:
method and displaying a custom view with a table when appropriate.
这篇关于像Safari浏览器的地址栏中自动建议的NSTextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!