按删除键后,UITextView caretRectForPosition错误值为最后一个字符

F79*_*F79 6 uitextview ipad uitextposition

我在Web视图中更改了文本的方法,以检测当前插入符号位置的可见rect.

UITextPosition *endPos = self.selectedTextRange.end;   
CGRect rect = [self caretRectForPosition:endPos];
[self scrollRectToVisible:rect animated:NO];
Run Code Online (Sandbox Code Playgroud)

除非我在文档末尾并按下键盘上的删除键,否则它的效果很好.在这种情况下,它会滚动到文档的开头,这是意外的.

Lab*_*Ars 1

我遇到了类似的问题......这似乎是文本视图中的计时问题。我的解决方案是:

A:检测caretRectForPosition的无效结果。就我而言,无效坐标似乎总是要么是大负值(-1.0 似乎是 iO!),要么是 origin.y 的“无限”。

B:短时间后重新向文本视图询问插入符位置。我检查了一些延迟值;0.05 似乎就足够了。

代码:

- (void)textViewDidChange:(UITextView *)pTextView {

    UITextPosition* endPos = pTextView.selectedTextRange.end;

    CGRect          caretRectInTextView = [pTextView caretRectForPosition:endPos];

    if ((-1.0 > CGRectGetMinY(caretRectInTextView)) ||
        (INFINITY == CGRectGetMinY(caretRectInTextView))) {
        NSLog(@"Invalid caretRectInTextView detected!");

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)),
                       dispatch_get_main_queue(),
                       ^{
                           // Recall
                           [self textViewDidChange:pTextView];
                        });
        return;


    }

    ... your code ...
}
Run Code Online (Sandbox Code Playgroud)