美文网首页
iOS -UICollectionView中Cell对齐方式,c

iOS -UICollectionView中Cell对齐方式,c

作者: CDLOG | 来源:发表于2018-08-07 00:25 被阅读887次

参考的这篇文章UICollectionView中Cell左对齐 居中 右对齐 等间距
这篇文章有对齐方式,但是没有根据文字算出cell 大小,这里给予补充

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"加载 cell");
    return  [self getMultiLineWithFont:14 andText: [self.dataArray objectAtIndex:[indexPath item]].content];
}
//根据文字计算cell大小
- (CGSize)getMultiLineWithFont:(NSInteger)font andText:(NSString *)text
{
    CGSize size  = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil].size;
    return size;
}

UICollectionView代理方法的加载顺序 先布局 cell 大小 再加载cell 数据
如果 cell 大小为0,是不会执行加载 cell 数据的方法的
github传送门

相关文章

网友评论

      本文标题:iOS -UICollectionView中Cell对齐方式,c

      本文链接:https://www.haomeiwen.com/subject/evpnvftx.html