自动完成在文本字段叽叽喳喳的用户名(可可) [英] Autocomplete with twitter usernames in text field (cocoa)

查看:147
本文介绍了自动完成在文本字段叽叽喳喳的用户名(可可)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看NSTokenField,和的NSTextField与NSTextView没有运气做到以下几点:

I was looking at NSTokenField, NSTextField and NSTextView with no luck to do the following:

我写一个Twtitter客户,当你希望Twitter上新建推,然后开始在文本字段中写例如:

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:

将会使咖啡,@pe

当你开始写 @ 那么我想帮助用户自动完成,例如用户名 @peter 。我有一个NSArray中的用户名,如:

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 *用户名= [NSArray的arrayWithObjects:@@安地列斯,@@克拉拉,@@ jeena,@@彼得]

我应该怎么做,使简单的autocompletation?我会很高兴,如果你将不得不preSS F5或东西对于初学者也。我遇到的问题是,与NSTokenField我不知道我应该怎么记号化的字符串,它的NSTextField只有当我写的@username在鸣叫开始和NSTextView似乎是这样一个简单的非常复杂和太多的工作的事情。

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.

推荐答案

最基本的实现涉及重写此方法.​​.....肯定不是最优的,但你应该明白了吧:

The most basic implementation involves overriding this method... Definitely not optimal, but you should get the idea:

- (NSArray *) completionsForPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index {
    // this would be defined somewhere else, but just for example.. 
    NSArray *usernames = [NSArray arrayWithObjects:@"@andreas", @"@clara", @"@jeena", @"@peter"];

    NSMutableArray *matchedNames = [NSMutableArray array];
    NSString *toMatch = [[self string] substringWithRange:charRange];
    for(NSString *username in usernames) {
        [matchedNames addObject:username];
    }

    return matchedNames; // that's it. 
}

一旦你开始有大量的数据,你需要通过存储的话到哈希有文本在他们的部分作品(聘用策略,以pre-做你的搜索,如你好就放成4个不同的阵列塞给的NSDictionary 为H,他,赫尔,地狱键..在你的词汇每一个字重复。快得多了方式。

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.

这篇关于自动完成在文本字段叽叽喳喳的用户名(可可)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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