iOS7中的UITextView剪辑文本字符串的最后一行

rya*_*yan 54 uitextview ios7

iOS7中的UITextView非常奇怪.当您键入并输入UITextView的最后一行时,滚动视图不会像应该的那样滚动到底部,这会导致文本被"剪切".我已经尝试将它的clipsToBound属性设置为NO,但它仍然剪辑文本.

我不想调用"setContentOffset:animated",因为对于一个:这是非常hacky的解决方案..其次:如果光标在我们的textview的中间(垂直),它将导致不必要的滚动.

这是一个截图.

在此输入图像描述

任何帮助将不胜感激!

谢谢!

dav*_*sdk 93

问题是由于iOS 7.在文本视图委托中,添加以下代码:

- (void)textViewDidChange:(UITextView *)textView {
    CGRect line = [textView caretRectForPosition:
        textView.selectedTextRange.start];
    CGFloat overflow = line.origin.y + line.size.height
        - ( textView.contentOffset.y + textView.bounds.size.height
        - textView.contentInset.bottom - textView.contentInset.top );
    if ( overflow > 0 ) {
        // We are at the bottom of the visible text and introduced a line feed, scroll down (iOS 7 does not do it)
        // Scroll caret to visible area
        CGPoint offset = textView.contentOffset;
        offset.y += overflow + 7; // leave 7 pixels margin
        // Cannot animate with setContentOffset:animated: or caret will not appear
        [UIView animateWithDuration:.2 animations:^{
            [textView setContentOffset:offset];
        }];
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 21

我在这里找到的解决方案是在创建UITextView后添加一行修复:

self.textview.layoutManager.allowsNonContiguousLayout = NO;
Run Code Online (Sandbox Code Playgroud)

这一行解决我在iOS7上创建了一个基于UITextView的代码编辑器的三个问题,其中包含语法高亮:

  1. 滚动以在编辑时将文本保留在视图中(此帖子的问题)
  2. UITextView在解除键盘后偶尔会跳转
  3. 尝试滚动视图时,UITextView随机滚动跳转

注意,当键盘显示/隐藏时,我确实调整了整个UITextView的大小.


chr*_*ris 6

尝试-textViewDidChangeSelection:从UITextViewDelegate 实现委托方法,如下所示:

-(void)textViewDidChangeSelection:(UITextView *)textView {
    [textView scrollRangeToVisible:textView.selectedRange];
}
Run Code Online (Sandbox Code Playgroud)