NSDictionary *attrsDictionary = [NSDictionarydictionaryWithObject:[UIFontsystemFontOfSize:kCellContentFontSize] forKey:NSFontAttributeName]; NSAttributedString *attributedText = [[[NSAttributedString alloc] initWithString:_contentStr attributes:attrsDictionary] autorelease]; NSInteger detailHeight = [BZDataDealer textViewHeightForAttributedText:attributedText andWidth:detailLabelWidth]; UITextView *detailTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 35, detailLabelWidth, detailHeight+3)]; // 加3个像素 detailTextView.scrollEnabled = NO; // 是否容许滚动会影响高度的展现 detailTextView.text = _contentStr; [containerView addSubview:detailTextView];
+ (NSInteger)textViewHeightForAttributedText:(NSAttributedString *)text andWidth:(CGFloat)width { UITextView *textView = [[UITextView alloc] init]; [textView setAttributedText:text]; CGSize size = [textView sizeThatFits:CGSizeMake(width, FLT_MAX)]; return (NSInteger)(size.height); }
之前的方法用 先赋值, 再取textview的contentsize.height, iOS7后, 这个不能和了.spa
苹果目前不少状况下都是推荐使用 NSAttributedString 来计算一些数据.code
ps: 若是 blog
detailTextView.scrollEnabled = NO; // 是否容许滚动会影响高度的展现
不容许滚动, 计算出的高度仍是会少一点. 因此上面加了3个像素.it
不知道是否是苹果你妹的bug.io