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

- (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];
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用scrollRangeToVisible函数进行滚动,可以跳动到最后一行内容上 [_textView scrol...
    情来情去情随缘阅读 4,661评论 1 0
  • 暑期带小儿学习经典,重读《诗经》。“桃之夭夭,灼灼其华;之子于归,宜其室家。”面若桃花的女子,带着青春靓丽的...
    水心水心阅读 322评论 0 1
  • 星期五 阴雨 每日一我 今天觉得特别累,起床起晚了。早读没完成,错过了纠音。 早饭吃了牛杂粉丝汤加生煎,中午上普拉...
    sophietyl阅读 143评论 0 1
  • 六年,像一阵风,徐徐吹过;像你陪在我身边,任劳任怨,记得六年我身边总会有朋友支持我,鼓励我,为我加油,,也会有...
    千羽杊阅读 308评论 0 0