Is there a way to implement custom Auto-completion for a UITextView, say by giving it a NSDictionary
or NSArray
of Strings to watch out for?
views:
45answers:
1You would have to program it yourself. If you implement the UITextViewDelegate Protocol, the function
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)
is called every time the user enters / deletes a character in the text view. So for example if the user enters a char 's', then in this function you would check the array w the words you want to autocomplete to see if it should autocomplete.
CONDITIONS -
• If there is only one value that starts with 's', autocomplete.
• If there are no values that start with 's', or if there are multiple values that start with 's', (ELSE) do not autocomplete.
I recommend that your autocomplete string array is sorted alphabetically, and that you keep a global variable that points to where you left off in the array. For example if the user enters 's', and the first word with 's' is in array index 5, then when the user enters another char, 'u' making the search string "su", you should be able to remember to start in array index 5 to find the autocomplete string faster (and not iterate through useless data). I would use a C array for this, although an NSArray would work.