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