//定義宏:
#define kFontSize 14
#define kPhotoCell_Width 300
#define kPhotoCell_MarginBetween 3
#define kPhotoCell_TitleLabel_Height 25
//方法:+ (CGFloat)heightForRowWithModel:(PhotoInfo *)photoInfo
{
//1.圖片的高度
//讓圖片等比例縮放
//(1)獲取圖片
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TSummer" ofType:@"png"]];
//(2)計算圖片的高度
CGFloat imageHeight = [self heightForImage:image];
//2.文本的高度
CGFloat textHeight = [self heightForText:photoInfo.introduction];
//3.返回cell 的總高度
return kPhotoCell_TitleLabel_Height + imageHeight + textHeight + 4 * kPhotoCell_MarginBetween;
}
//單獨計算圖片的高度
+ (CGFloat)heightForImage:(UIImage *)image
{
//(1)獲取圖片的大小
CGSize size = image.size;
//(2)求出縮放比例
CGFloat scale = kPhotoCell_Width / size.width;
CGFloat imageHeight = size.height * scale;
return imageHeight;
}
//單獨計算文本的高度
+ (CGFloat)heightForText:(NSString *)text
{
//設置計算文本時字體的大小,以什麼標准來計算
NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:kFontSize]};
return [text boundingRectWithSize:CGSizeMake(kPhotoCell_Width, 1000) options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) attributes:attrbute context:nil].size.height;
}