-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
SMTReportItemModel *tagModel=self.model.list[indexPath.item];
CGSize size = [AppDelegateUM sizeWithText:tagModel.title font:MEDIUMSYSTEMFONT(13) maxSize:CGSizeMake(kCellWidth*4+40, kCellHeight)];
return CGSizeMake(size.width+20,kCellHeight);
}
我找了半天,代码如上。只要实现该方法即可。
计算好文字宽度,然后返回宽度和高即可。该组件会自动排列。但是会左右对齐,中间可能有空隙。
后来,我继续研究,发现 要做到只是左对齐,需要编写一个继承类,相关位置重新定义即可。
具体可以参考如下URL:
https://www.jianshu.com/p/84732f9af195