UITextView自定义自动完成 [英] UITextView Custom Auto-completion

查看:171
本文介绍了UITextView自定义自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法为UITextView实现自定义自动完成,例如给它一个 NSDictionary NSArray

解决方案

你必须自己编程。如果你实现UITextViewDelegate协议,函数

   - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange: replaceText:(NSString *)



每当用户在文本视图中输入/删除字符时, 。因此,例如,如果用户输入一个char's',那么在这个函数中,你将检查数组w你想要自动完成的单词,看看是否应该自动完成。



条件 -



•如果只有一个以s开头的值, p>

•如果没有以s开头的值,或者有多个以s开头的值,则(ELSE)不会自动完成。

我建议您的自动完成字符串数组按字母顺序排序,并保留一个全局变量指向您在数组中停止的位置。例如,如果用户输入s,并且带有s的第一个单词在数组索引5中,则当用户输入另一个字符时,u使搜索字符串su,您应该能够记住在数组索引5中启动以更快地找到自动完成字符串(而不是通过无用的数据迭代)。我将使用一个C数组为此,虽然一个NSArray将工作。


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?

解决方案

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.

这篇关于UITextView自定义自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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