实现textView滚动到某个区域的方式
1.滚动到某矩形可是区域
[self.textView scrollRectToVisible:CGRectMake(0, self.textView.contentSize.height - 10, self.textView.contentSize.width, 10) animated:NO];
2.滚动到顶部或底部
//滚动到底部
CGPoint bottomOffset = CGPointMake(0, self.textView.contentSize.height - self.textView.bounds.size.height);
if(bottomOffset.y > 0){
[self.textView setContentOffset:bottomOffset animated:NO];
}
//滚动到顶部
[self.textView setContentOffset:CGPointZero animated:YES];
3、滚动到指定范围
NSRange range = NSMakeRange(10, 20); // 从第10个字符开始,长度为20的范围
[self.textView scrollRangeToVisible:range];
二、实践出真理
先定义并懒加载textView
@property (nonatomic, strong) UITextView *textView;
- (UITextView *)textView{
if(!_textView){
_textView = [[UITextView alloc] initWithFrame:self.backView.bounds];
_textView.showsVerticalScrollIndicator = NO;
_textView.showsHorizontalScrollIndicator = NO;
_textView.automaticallyAdjustsScrollIndicatorInsets = NO;
_textView.textContainerInset = UIEdgeInsetsMake(12, 20, 12, 20);
_textView.bounces = YES;
_textView.layoutManager.allowsNonContiguousLayout = NO;
}
return _textView;
}
我使用下面这种方式实现滚动:
[self.textView scrollRectToVisible:CGRectMake(0, self.textView.contentSize.height - 10, self.textView.contentSize.width, 10) animated:NO];
问题?:如何只是一次滚动一般不会有什么问题,但是需要逐字输出并且滚动到底部,那每次拼接一个字的时候就会执行一次滚动,当文本越长,滚动出现的闪烁越明显越剧烈。
主要是这句 _textView.layoutManager.allowsNonContiguousLayout = NO;起了作用 。设置其连续性布局为No。就不会出现闪烁的问题了。