本文介绍了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因为一个:这是非常hacky的解决方案..其次:如果光标位于中间(垂直)在我们的textview中,它会导致不必要的滚动。

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.

这是截图。

任何帮助都将不胜感激!

Any help would be greatly appreciated!

谢谢!

推荐答案

问题是由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剪辑文本字符串的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:32