UICollectionViewCell 的动态高度 [英] Dynamic height for UICollectionViewCell

查看:29
本文介绍了UICollectionViewCell 的动态高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要根据 TextView 中的文本动态单元格大小,这是我的代码

I want dynamic cell Size as per text in TextView, Here is my code

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
        {
            return DataArray.count
        }
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
        {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CustomCollectionViewCell

            let fixedWidth = cell?.TextView.frame.size.width
            cell?.TextView.sizeThatFits(CGSize(width: fixedWidth!, height: CGFloat.greatestFiniteMagnitude))
            let newSize = cell?.TextView.sizeThatFits(CGSize(width: fixedWidth!, height: CGFloat.greatestFiniteMagnitude))
            var newFrame = cell?.TextView.frame
            newFrame?.size = CGSize(width: max((newSize?.width)!, fixedWidth!), height: (newSize?.height)!)
            cell?.TextView.frame = newFrame!
            TextViewFrame = newFrame!
            cell?.TextView.text = DataArray[indexPath.row] as? String
            return cell!
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
        {

            return CGSize(width: CV.frame.size.width, height: 200)
        }

这里我为单元格提供了 200 个固定大小.它给了我如下输出它为小内容和大内容提供了单元格高度 200.但实际上我想要根据内容的单元格高度.我怎样才能做到这一点?任何帮助表示赞赏.提前致谢

Here i have given 200 fixed size for cell. it gives me output as below It gives me cell height 200 for small content as well as for large content . But actually i want cell height as per content. How can i achieve this? Any help is appreciated. Thanks in advance

推荐答案

一旦您知道要显示的文本的边界矩形,您就可以找到单元格的高度.

You can find the height of the cell once you know the bounding rect for the text you want to display.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    let text = "String that you want to show in the label inside the cell"
    let boundingRect = NSString(string: text).boundingRectWithSize(CGSizeMake(collectionView.bounds.width, 1000),
                                                                   options: .UsesLineFragmentOrigin,
                                                                   attributes: [NSFontAttributeName: UIFont.systemFontOfSize(11.0)],
                                                                   context: nil)

    let size = CGSizeMake(collectionView.bounds.width, ceil(boundingRect.height))

    return size
}

只要确保将 label 的上/下/左/右边距补到 cell.

Just be sure to make up for the top/bottom/left/right margins of your label to the cell.

这篇关于UICollectionViewCell 的动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆