ios - UITextView 在输入文字的高度超过UITextView的frame的时候的问题
PHPz
PHPz 2017-04-17 14:58:02
[iOS讨论组]

主要是中文的问题,就是当字符过多的时候,文字高度已经超过了textView。这个时候会滚动向下。

问题是,当输入中文,在打字的时候,此时是英文字符,textView的contentOffset是到最底部的。但是只要确定选择了中文,textView的conentOffset就会向上偏移一点,此时新输入的文字就有部分看不到了

PHPz
PHPz

学习是最好的投资!

全部回复(1)
高洛峰

在iOS7中,UITextView多了一个textContainer属性用来描述一些Layout。

在用

objcUITextView *textView = [[UITextView alloc] init];

这个方法时,并没用初始化textContainer,会导致输入中文时页面滚动出现bug。修复这个bug,需要用以下的方法初始化。

objc
@implementation SomeTextView -(id)init { if (IOS7) { NSTextStorage* textStorage = [[NSTextStorage alloc] init]; NSLayoutManager* layoutManager = [NSLayoutManager new]; [textStorage addLayoutManager:layoutManager]; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(someWidth, someHeight)]; textContainer.widthTracksTextView = YES; textContainer.heightTracksTextView = YES; [layoutManager addTextContainer:textContainer]; if (self = [super initWithFrame:CGRectZero textContainer:textContainer]) { } } else { if (self = [super init]) { } } return self; } - (id)initWithFrame:(CGRect)frame { if (IOS7) { NSTextStorage* textStorage = [[NSTextStorage alloc] init]; NSLayoutManager* layoutManager = [NSLayoutManager new]; [textStorage addLayoutManager:layoutManager]; NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:CGSizeMake(frame.size.width, kDeviceHeight)]; textContainer.widthTracksTextView = YES; textContainer.heightTracksTextView = YES; [layoutManager addTextContainer:textContainer]; if (self = [super initWithFrame:frame textContainer:textContainer]) { } } else { if (self = [super initWithFrame:frame]) { } } return self; } @end
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号