sim*_*eon 4 iphone cocoa-touch uitextview uitextviewdelegate ios
我遇到了一个问题,当按住键盘上的Delete键时,iOS给我的UITextViewDelegate提供了不正确的信息。
当用户HOLDS在UITextView中的删除键在iPad中的UITextView将开始删除整个单词,而不是它被按下单个字符长(注:这不会发生在模拟器)。
发生这种情况时,UITextView委托方法:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Run Code Online (Sandbox Code Playgroud)
被调用时,其范围包括正确的光标位置,但长度为1。这是不正确的,因为UITextView现在正在删除整个单词,而不是单个字母。例如,以下代码将只打印一个空格。
[textView substringWithRange:range]
string contains " "
Run Code Online (Sandbox Code Playgroud)
尽管UITextView删除了整个单词。替换文本正确指定为空字符串。是否有人知道解决此问题的方法或解决方法?
雅各布提到我应该发表这个答案。就是这样
我对此的棘手解决方法是监视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];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2442 次 |
| 最近记录: |