如何为动态文本获取CollectionView单元的统一高度

时间:2016-09-02 19:02:26

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个带动态单元的视图集。

我发现Cell a / c的高度为Text Supplied:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *)indexPath
{

CGFloat cellWidth = (self.mCollectionView.frame.size.width-7)/7;

CGFloat hegiht = [calendar getTheSizeForText:[dataArray objectAtIndex:indexPath.row] toFitInWidth:cellWidth];


return CGSizeMake(cellWidth,
                  hegiht) ;
 }

// FInding Height

   + (CGFloat)getTheSizeForText:(NSString *)text toFitInWidth:(CGFloat)width
{

CGRect textRect = [text boundingRectWithSize:CGSizeMake(width-2*kPadding, 999)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]}
                                     context:nil];

CGFloat cellHeight = textRect.size.height+10+21 ; //5 top padding 5  
     bottom 21 is the height of another label which is fied

return cellHeight;
}

现在,当我运行我的应用程序时,我正在获取屏幕:

enter image description here

我的细胞高度不均匀。获得统一身高的最佳方法是什么。

0 个答案:

没有答案