NSTextField与自动建议,如Safari的地址栏? [英] NSTextField with auto-suggestions like Safari's address bar?

查看:175
本文介绍了NSTextField与自动建议,如Safari的地址栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个 NSTextField 和一个推荐列表动态显示为用户类型的最简单的方法是什么?就像Safari的地址栏有一些菜单(我很相信Safari的地址栏建议是菜单,因为它有圆角,蓝色渐变选择和背景模糊)。



我已尝试使用。

解决方案

您需要子类化 NSTextView 并实现 - (NSRange)rangeForUserCompletion 方法,返回整个字符串的范围文本域。这应该确保它不只是自动完成最近输入的字。



如果你想要一个自定义菜单,你必须自己做,可能通过实现 -controlTextDidChange:方法并在适当时显示包含表格的自定义视图。


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).

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.

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:

  • Whenever a suggestion is selected, the entire text field is replaced with the suggestion.
  • Keep the suggestion list aligned with the text field's left side.

Note: This is not a question about adding progress indicator behind a text field.

解决方案

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.

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.

这篇关于NSTextField与自动建议,如Safari的地址栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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