转载自: https://github.com/zhangxiongwen/UITextView
- (void)textViewDidChange:(UITextView *)textView{
CGFloat height = 0;
CGFloat minHeight = 33; //最小的高度
CGFloat maxHeight = 168; //最大的高度
NSLog(@"%f",textView.contentSize.height);
if (textView.contentSize.height<minHeight) {
height = minHeight;
}else if (textView.contentSize.height>maxHeight){
height = maxHeight;
}else{
height = textView.contentSize.height;
}
//高度改变后的差值
CGFloat h = height - _textView.frame.size.height;
//改变frame
_textView.frame = CGRectMake(10, _textView.frame.origin.y-h, _textView.frame.size.width, height);
//加上这句话就不会出现闪一下的问题了
[self.textView scrollRangeToVisible:NSMakeRange(self.textView.text.length-1, 1)];
}