我遇到了一个问题,当删除键被保存在键盘上时,iOS给出了不正确的UITextViewDelegate信息。
当用户持有( iPad上的UITextView上的delete键)时,UITextView将开始删除整个单词,而不是单个字符(请注意:这在模拟器中不会发生)。
当发生这种情况时,UITextView委托方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text获取由正确的光标位置组成的范围调用,但长度为1。这是不正确的,因为UITextView现在正在删除整个单词,而不是单个字母。例如,下面的代码将只打印一个空格。
[textView substringWithRange:range]
string contains " "尽管UITextView删除了整个单词。替换文本被正确地指定为空字符串。有谁知道这个问题的解决方案或解决办法吗?
发布于 2012-02-07 00:43:40
雅各布说我应该把这个作为答案。所以就是这样了。
我的解决办法是监视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];
}
}https://stackoverflow.com/questions/6007997
复制相似问题