前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 7 之后UITextView 最后一行显示bug

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

作者头像
程序员不务正业
发布2018-06-13 16:44:49
8610
发布2018-06-13 16:44:49
举报
代码语言:javascript
复制
- (void) textViewDidChange:(UITextView *)textView
    {
    if (textView.text.length > 5000) { // 限制5000字内
        textView.text = [textView.text substringToIndex:5000];
    }

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

代码语言:javascript
复制
    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];
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.08.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档