动态改变textview的高度
2020-01-10 本文已影响0人
guoguojianshu
- (float) QiuTanTiYuHeightForTextView: (UITextView *)textView QiuTanTiYuWithText: (NSString *) strText{
CGSize constraint = CGSizeMake(textView.contentSize.width , CGFLOAT_MAX);
CGRect size = [strText boundingRectWithSize:constraint
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName: TYZXFANGSCMEDIUM(14)}
context:nil];
float textHeight = size.size.height + 22.0;
return textHeight;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
CGRect QiuTanTiYuFrame = textView.frame;
float QiuTanTiYuHeight = [self QiuTanTiYuHeightForTextView:textView QiuTanTiYuWithText:textView.text];
QiuTanTiYuFrame.size.height = QiuTanTiYuHeight;
[UIView animateWithDuration:0.5 animations:^{
textView.frame = QiuTanTiYuFrame;
}];
[self.QiuTanTiYuTextView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(QiuTanTiYuHeight);
}];
return YES;
}
-(void)textViewDidChange:(UITextView *)textView{
if (textView.text.length>0) {
self.QiuTanTiYuPlaceHolderLabel.hidden = YES;
}else{
self.QiuTanTiYuPlaceHolderLabel.hidden = NO;
}
}