扫码关注官方订阅号
主要是中文的问题,就是当字符过多的时候,文字高度已经超过了textView。这个时候会滚动向下。
问题是,当输入中文,在打字的时候,此时是英文字符,textView的contentOffset是到最底部的。但是只要确定选择了中文,textView的conentOffset就会向上偏移一点,此时新输入的文字就有部分看不到了
学习是最好的投资!
在iOS7中,UITextView多了一个textContainer属性用来描述一些Layout。
UITextView
textContainer
在用
objcUITextView *textView = [[UITextView alloc] init];
objc
UITextView *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
@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
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
在iOS7中,
UITextView多了一个textContainer属性用来描述一些Layout。在用
这个方法时,并没用初始化textContainer,会导致输入中文时页面滚动出现bug。修复这个bug,需要用以下的方法初始化。