刚刚在弄一个填写地址的输入框,输入框的高度随文字变化,想了一下用TextView,看了一下各种开源的代码,觉得好麻烦,自己简单写了一下,还算是好用,下面上代码
- (void)textViewDidChange:(UITextView *)textView{
}CGSize constraintSize = CGSizeMake(_textView.width, MAXFLOAT);
CGSize size = [_textView sizeThatFits:constraintSize];
CGFloat currentHeight = size.height;
CGFloat maxHeight = _textView.font.lineHeight*4;//限制最大行数为4行
if(currentHeight > maxHeight){
currentHeight = maxHeight;
_textView.scrollEnabled = YES;
}else{
_textView.scrollEnabled = NO;
}
_textView.height = currentHeight;
原理很简单,就是让textView自适应,然后我们简单的改变高度就可以了