首次布局获取的高度不对需要设置
- (CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutsizeForItemAtIndexPath:(NSIndexPath*)indexPath
{
NSString*str = [NSStringstringWithFormat:@"%@",self.arr[indexPath.row][@"title"]];
CGFloat wid = [self getStringWidthWithText:str];
returnCGSizeMake(wid +25,35);
}
- (CGFloat)getStringWidthWithText:(NSString *)text{
// 设置文字属性 要和label的一致
NSDictionary*attrs =@{NSFontAttributeName:DEF_FONT_SIZE(13)};
CGSizemaxSize =CGSizeMake(MAXFLOAT,35);
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
// 计算文字占据的宽高
CGSize size = [text boundingRectWithSize:maxSize options:options attributes:attrs context:nil].size;
// 当你是把获得的高度来布局控件的View的高度的时候.size转化为ceilf(size.height)。
return ceilf(size.width);
}
然后在获取高度
[self.goodCollectionView layoutIfNeeded];
return self.goodCollectionView.collectionViewLayout.collectionViewContentSize.height;