剪辑最后一行文本字符串

剪辑最后一行文本字符串

本文介绍了iOS7 中的 UITextView 剪辑最后一行文本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

UITextView in iOS7 has been really weird. As you type and are entering the last line of your UITextView, the scroll view doesn't scroll to the bottom like it should and it causes the text to be "clipped". I've tried setting it's clipsToBound property to NO but it still clips the text.

我不想调用setContentOffset:animated",因为一方面:这是一个非常笨拙的解决方案.其次:如果光标位于我们文本视图的中间(垂直),它会导致不必要的滚动.

I don't want to call on "setContentOffset:animated" because for one: that's very hacky solution.. secondly: if the cursor was in the middle (vertically) of our textview, it'll cause unwanted scrolling.

这是截图.

任何帮助将不胜感激!

谢谢!

推荐答案

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

The problem is due to iOS 7. In the text view delegate, add this code:

- (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];
        }];
    }
}

这篇关于iOS7 中的 UITextView 剪辑最后一行文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:01