- 创建一个uitextview的视图对象并设置代理
- 实现代理方法:
高度自适应
-(void)textViewDidChange:(UITextView *)textView{
//设置最大的高度
static CGFloat maxHeight = 500;
CGFloat width = textView.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(width,MAXFLOAT)];
CGRect newFrame = textView.frame;
//大于对大高度是设置为可滚动
if (newSize.height>=maxHeight) {
newSize.height = maxHeight;
textView.scrollEnabled = YES;
}else{
textView.scrollEnabled = NO;
}
newFrame.size = newSize; textView.frame= newFrame;
}
点击return时隐藏键盘
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}