//orgSize设置宽度和高度,将需要得到的高度或宽度设置为一个很大的值
- (CGFloat)heightForTextView:(UITextView *)textView OrgSize:(CGSize)orgSize{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
//需IOS7.0
NSDictionary *dicFont = [NSDictionary dictionaryWithObjectsAndKeys:textView.font, NSFontAttributeName, nil];
CGSize size = [textView.text boundingRectWithSize:orgSize
options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
attributes:dicFont
context:nil].size;
return size.height;
} else {
//IOS7.0以下
return textView.contentSize.height;
}
return 0;
}
//same
<pre name="code" class="objc">CGFloat fixedWidth = self.view.frame.size.width;
CGSize newSize = [label sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
CGFloat height = newSize.height;
动态设置TextView的高度
最新推荐文章于 2022-07-22 14:35:27 发布