美文网首页
iOS 7 之后UITextView 最后一行显示bug

iOS 7 之后UITextView 最后一行显示bug

作者: 程序员不务正业 | 来源:发表于2017-08-23 16:04 被阅读13次
- (void) textViewDidChange:(UITextView *)textView
    {
    if (textView.text.length > 5000) { // 限制5000字内
        textView.text = [textView.text substringToIndex:5000];
    }
    

iOS7之后出现,通过改变scrolloview的setContentOffset处理位置

    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 += 5; // leave 5 pixels margin
        // Cannot animate with setContentOffset:animated: or caret will not appear
        [UIView animateWithDuration:.2 animations:^{
            [_textView setContentOffset:offset];
        }];
    }
   
    [self setTextViewSizeToFit:NO];
    }

相关文章

网友评论

      本文标题:iOS 7 之后UITextView 最后一行显示bug

      本文链接:https://www.haomeiwen.com/subject/nezddxtx.html