//計算集合視圖的高度
- (CGFloat)calculateHeightOfCollectionViewForTexts:(NSArray*)texts{
//集合視圖的寬度
CGFloat viewWidth = SCREEN_WIDTH - 12 - 12;
//設(shè)置文本之間的間隔
CGFloat textInterval = 10.f;
//計算多個文本寬度和
CGFloat textWidthSum = textInterval;
//設(shè)置文本的行高
CGFloat textHeight = 34.f;
//設(shè)置文本計數(shù)
NSInteger textCount = 0;
//記錄行數(shù)
NSInteger numberRow = 0;
for (NSString *str in texts) {
textCount ++;
textWidthSum = textWidthSum + [self calculateWidthOfTextForStr:str] + textInterval;
if (textWidthSum > viewWidth) {
numberRow ++;
textWidthSum = textInterval + [self calculateWidthOfTextForStr:str];
textCount = 1;
}
if ([str isEqualToString:texts.lastObject]) {
numberRow ++;
}
}
return textHeight*numberRow + 10*(numberRow-1) + 20;
}