开发者

UITextView Custom Auto-completion

Is there a way to implement custom Auto-completion for a UITextView, say by giving it a NSDi开发者_Python百科ctionary or NSArray of Strings to watch out for?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜