在文本字段中自动完成 Twitter 用户名(可可)
我正在查看 NSTokenField、NSTextField 和 NSTextView,但没有运气执行以下操作:
当您想发布一条新推文时,您可以开始在文本字段中写入,例如:
我正在编写一个 Twtitter 客户端, 咖啡,@pe
当您开始编写@
时,我想帮助用户自动完成用户名,例如@peter
。我有一个 NSArray,其用户名如下:
NSArray *usernames = [NSArray arrayWithObjects:@"@andreas", @"@clara", @"@jeena", @"@peter"]
应该做什么我要启用简单的自动完成功能吗?如果您也必须按 F5 或其他启动键,我会很高兴。我遇到的问题是,使用 NSTokenField 我不知道如何标记字符串,使用 NSTextField 仅当我在推文开头写入 @username 时才有效,而 NSTextView 似乎非常复杂,对于如此简单的操作来说太多了事物。
I was looking at NSTokenField, NSTextField and NSTextView with no luck to do the following:
I am writing a Twtitter client and when you want to twitter a new Tweet then you begin to write in a text field for example:
Going to make coffee, @pe
When you begin to write a @
then I would like to help the user to autocomplete the username for example @peter
. I have a NSArray with the usernames like:
NSArray *usernames = [NSArray arrayWithObjects:@"@andreas", @"@clara", @"@jeena", @"@peter"]
What should I do to enable a simple autocompletation? I'd be happy if you would have to press F5 or something for starters too. The problem I am having is that with NSTokenField I don't know how I should tokenize the string, with NSTextField it only works when I write the @username at the beginning of the tweet and NSTextView seems really complicated and too much for such a simple thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最基本的实现涉及重写此方法......绝对不是最佳的,但您应该明白:
一旦您开始拥有大量数据,您将需要采用策略通过将单词存储到哈希中来预先执行搜索其中的部分文本(例如,“Hello”将被放入 4 个不同的数组中,内容放入 NSDictionary 键中,表示“H”、“He”、“Hel”、“Hell” .. 重复与你词典中的每一个单词。这样速度会快得多,
如果您想支持自动完成,只需在检测到控件中的文本发生更改时调用“complete:”方法即可。
The most basic implementation involves overriding this method... Definitely not optimal, but you should get the idea:
Once you start having a lot of data, you'll need to employ strategies to pre-do your searches by storing words into hashes with the partial pieces of text in them (like, "Hello" would be put into 4 different arrays stuff into
NSDictionary
keys for "H", "He", "Hel", "Hell" .. Repeat with every word in your Lexicon. Much quicker that way.If you want to support auto-complete, just call the 'complete:' method when you detect text is changing in your control.