UICollectionViewCell的自动宽度大小-Swift
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UICollectionViewCell的自动宽度大小-Swift相关的知识,希望对你有一定的参考价值。
我如何自动将UICollectionViewCell
大小的基本大小width
设为string
?
答案
您可以执行此操作以根据字符串计算宽度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)
}
以上是关于UICollectionViewCell的自动宽度大小-Swift的主要内容,如果未能解决你的问题,请参考以下文章