计算文字的高度:
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define HomePage_Collection_Cell_Title_FrameWidth ((kScreenWidth - 25)/2.0 - 15 - 22)
- (double)calculateTitleHeight:(NSString*)strTitle {
NSLog(@"calculateTitleHeight: %@", strTitle);
// 计算出每个Cell的高度
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:strTitle];
UIFont *font = [UIFont systemFontOfSize:17.0];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
// TODO: 行间距可以设置成动态的
style.lineSpacing = 2; // [self.lineSpaceTextField.text integerValue];
[attributeString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, strTitle.length)];
[attributeString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, strTitle.length)];
// self.showTextLabel.attributedText = attributeString;
//
// // NSLog(@"textView.text.length:%lf", textView.text.length)
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
CGFloat boundingRectHeight = [attributeString boundingRectWithSize:CGSizeMake(HomePage_Collection_Cell_Title_FrameWidth, MAXFLOAT) options:options context:nil].size.height;
if ([attributeString.string isEqualToString:@""] || [attributeString.string isEqualToString:@"(null)"]) {
boundingRectHeight = 0;
}
NSLog(@"boundingRectHeight: %lf", boundingRectHeight);
return boundingRectHeight;
}
网友评论