UICollectionViewCell 动态宽度和固定高度
UICollectionViewCell dynamic width and fixed height
我需要将 UICollectionView 单元格制作成椭圆形,其中高度固定但宽度是动态的,并且它也有限制,如果文本长于该值,则文本应滚动。为此可用的任何第三方选项或需要使用 UICollectionView 创建自己的选项。请指导。
下面是我想要实现的图像。我想知道在开始之前我应该寻找第三方还是使用 UICollectionView 自己制作。我有很短的时间来完成这就是为什么要避免在开始搜索时询问 follow.Please 指导的方向。
您可以使用 UICollectionViewFlowLayout 和自动布局来实现此目的。
- 创建一个带有容器视图的 UICollectionViewCell。
- 使用自动布局固定此容器视图单元格的边缘
- 向这个容器视图添加一个 UILabel 并将它固定到容器视图的所有边缘(给它一个背景颜色以区别于单元格背景)
- 在 UICollectionViewCell 子类中,您需要圆化容器视图的角,例如
self.containerView.layer.cornerRadius = self.containerView.height / 2
- 在 UICollectionViewFlowLayoutDelegate 方法中,estimatedSizeForItem return 单元格的近似大小(自动布局将计算实际大小。)
要记住的重要一点是您的单元格需要有足够的约束,以便自动布局引擎可以根据内容计算实际的高度和宽度。
编辑:如果您想要固定高度,请确保您的标签只能有一行。或添加高度限制。
最后,我找到了一个可以通过 cocoapods 安装的库 TagListView,它有很多自定义功能,并且还支持 swift 4。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let nw = intersts[indexPath.row]
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let size = CGSize(width: 250, height: 1500)
let estimatedFrame = NSString(string: nw).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)], context: nil)
let attributes = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)]
let yourLabelSize: CGSize = nw.size(withAttributes:attributes )
var width1 = yourLabelSize.width + 30
if width1 < 30 {
width1 = 30
}
return CGSize(width: estimatedFrame.width+20, height: estimatedFrame.height+20)
}
我只是扩展或实现了@Tim 的回答。因此,在按照他的回答中的描述构建单元格之后,然后使用下面的示例代码灵活指定单元格的宽度和高度,即类似于他描述的
let collectionViewLayout = UICollectionViewFlowLayout()
collectionViewLayout.scrollDirection = .horizontal
let itemWidth = Constants.myCollectionViewItemWidth
let itemHeight = Constants.myCollectionViewItemHeight
collectionViewLayout.estimatedItemSize = CGSize(width: itemWidth, height: itemHeight)
myCollectionView.collectionViewLayout = collectionViewLayout
我需要将 UICollectionView 单元格制作成椭圆形,其中高度固定但宽度是动态的,并且它也有限制,如果文本长于该值,则文本应滚动。为此可用的任何第三方选项或需要使用 UICollectionView 创建自己的选项。请指导。
下面是我想要实现的图像。我想知道在开始之前我应该寻找第三方还是使用 UICollectionView 自己制作。我有很短的时间来完成这就是为什么要避免在开始搜索时询问 follow.Please 指导的方向。
您可以使用 UICollectionViewFlowLayout 和自动布局来实现此目的。
- 创建一个带有容器视图的 UICollectionViewCell。
- 使用自动布局固定此容器视图单元格的边缘
- 向这个容器视图添加一个 UILabel 并将它固定到容器视图的所有边缘(给它一个背景颜色以区别于单元格背景)
- 在 UICollectionViewCell 子类中,您需要圆化容器视图的角,例如
self.containerView.layer.cornerRadius = self.containerView.height / 2
- 在 UICollectionViewFlowLayoutDelegate 方法中,estimatedSizeForItem return 单元格的近似大小(自动布局将计算实际大小。)
要记住的重要一点是您的单元格需要有足够的约束,以便自动布局引擎可以根据内容计算实际的高度和宽度。
编辑:如果您想要固定高度,请确保您的标签只能有一行。或添加高度限制。
最后,我找到了一个可以通过 cocoapods 安装的库 TagListView,它有很多自定义功能,并且还支持 swift 4。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let nw = intersts[indexPath.row]
let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
let size = CGSize(width: 250, height: 1500)
let estimatedFrame = NSString(string: nw).boundingRect(with: size, options: options, attributes: [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)], context: nil)
let attributes = [NSAttributedStringKey.font : UIFont.systemFont(ofSize: 17)]
let yourLabelSize: CGSize = nw.size(withAttributes:attributes )
var width1 = yourLabelSize.width + 30
if width1 < 30 {
width1 = 30
}
return CGSize(width: estimatedFrame.width+20, height: estimatedFrame.height+20)
}
我只是扩展或实现了@Tim 的回答。因此,在按照他的回答中的描述构建单元格之后,然后使用下面的示例代码灵活指定单元格的宽度和高度,即类似于他描述的
let collectionViewLayout = UICollectionViewFlowLayout()
collectionViewLayout.scrollDirection = .horizontal
let itemWidth = Constants.myCollectionViewItemWidth
let itemHeight = Constants.myCollectionViewItemHeight
collectionViewLayout.estimatedItemSize = CGSize(width: itemWidth, height: itemHeight)
myCollectionView.collectionViewLayout = collectionViewLayout