如何自动将UICollectionViewCell
大小的基本大小width
设为string
?
答案 0 :(得分:1)
您可以执行此操作以根据字符串计算宽度
let padding = 30
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let lbl = UILabel()
lbl.text = arr[indexPath.item] // arr = [String]()
let si = lbl.sizeThatFits(CGSize(width:CGFloat.greatestFiniteMagnitude, height:40))
print(si.width)
return CGSize(width:si.width + padding , height:40)
}
class VCName:UIViewController , UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout { }