当退格键为HELD Down时的UITextViewDelegate行为

我遇到了一个问题,当键盘上有删除键时,iOS会给我的UITextViewDelegate提供不正确的信息。

当用户在iPad上的UITextView上HOLDS删除键时,UITextView将开始删除整个单词而不是单个字符的时间越长(注意:这在模拟器中不会发生)。

发生这种情况时,UITextView委托方法:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 

使用由正确的光标位置组成的范围调用,但长度为1.这是不正确的,因为UITextView现在正在删除整个单词,而不是单个字母。 例如,以下代码将仅打印一个空格。

 [textView substringWithRange:range] string contains " " 

尽管UITextView删除了整个单词。 替换文本正确地作为空字符串给出。 有没有人知道这个问题的解决方案或解决方法?

雅各布提到我应该发布这个答案。 所以在这里。

我的hackish解决方法是监视shouldChangeTextInRange中给出的文本长度和范围,然后将其与textViewDidChange中文本的长度进行比较。 如果差异不同步,请刷新我的支持文本缓冲区并从文本视图重建它。 这不是最佳的。 这是我的临时解决方法:

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { //Push the proposed edit to the underlying buffer [self.editor.buffer changeTextInRange:range replacementText:text]; //lastTextLength is an NSUInteger recording the length that //this proposed edit SHOULD make the text view have lastTextLength = [textView.text length] + ([text length] - range.length); return YES; } - (void)textViewDidChange:(UITextView *)textView { //Check if the lastTextLength and actual text length went out of sync if( lastTextLength != [textView.text length] ) { //Flush your internal buffer [self.editor.buffer loadText:textView.text]; } }