自适应高度的输入框,这里直接上代码
在textView的代理方法中写入下面的代码就可以实现了
-(void)textViewDidChange:(UITextView *)textView{
static CGFloat maxHeight =60.0f;
CGRect frame = textView.frame;
CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
CGSize size = [textView sizeThatFits:constraintSize];
if (size.height > frame.size.height) {
if (size.height >= maxHeight)
{
size.height = maxHeight;
textView.scrollEnabled = YES; // 允许滚动
}
else
{
textView.scrollEnabled = NO; // 不允许滚动
}
textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
}