Swift给出uicollectionviewcell宽度和动态高度

时间:2017-08-13 18:59:30

标签: ios swift uicollectionview

我有这个uicollectionviewcell

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = commentSection.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CommentCell
    return cell
}

CommentCell

let CommenterprofileImage: UIImageView = {
        let v = UIImageView()
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()
    let commentText: UILabel = {
        let l = UILabel()
        l.numberOfLines = 0
        l.text = "some text"
        l.translatesAutoresizingMaskIntoConstraints = false
        return l
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .blue
        designCell()
    }
    func designCell(){

        addSubview(CommenterprofileImage)
        addSubview(commentText)

        addCellConstraints()
    }

    func addCellConstraints(){  
        CommenterprofileImage.topAnchor.constraint(equalTo: self.topAnchor,constant:10).isActive = true
        CommenterprofileImage.leftAnchor.constraint(equalTo: self.leftAnchor,constant:20).isActive = true
        CommenterprofileImage.widthAnchor.constraint(equalToConstant: 70).isActive = true
        CommenterprofileImage.heightAnchor.constraint(equalToConstant: 70).isActive = true

        commentText.topAnchor.constraint(equalTo: CommenterprofileImage.bottomAnchor,constant:20).isActive = true
        commentText.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        commentText.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
    }

我希望这个单元格的宽度等于view的宽度,但我希望它的高度取决于它的内容。 我试过这个

layout.estimatedItemSize = CGSize(width: view.frame.width, height: 100)

但是我的格子高度为100,宽度小于100。

1 个答案:

答案 0 :(得分:0)

你可以这样做 - :

首先使用boundingRect方法计算标签高度 -

func estimatedFrameHeight(text:String) -> CGRect{
        let size = CGSize(width: (view.frame.width/2 - 8), height: 1000)
        let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
        let attribute = [NSFontAttributeName:UIFont.systemFont(ofSize: 11)]
        return NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil)
    }

保持与标签完全相同的字体,并保留.usesLineFragmentOrigin多行标签。

现在在CollectionView sizeForItemAt indexPath中执行此操作 - :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (collectionView.frame.width)
        let subDict = subCategoryListArray[indexPath.row]
        if let product_name = subDict["product_name"] as? String {
        let height = estimatedFrameHeight(text:product_name)
        return CGSize(width: width, height: height.height + 70 + 10 + 20)
    }
        return CGSize(width: 0, height: 0)

    }

let height = estimatedFrameHeight(text:product_name)。如果需要,这将为您提供估计的高度和宽度。现在你计算了你的标签高度,你需要添加imageView高度,y位置约束才能使它工作。你可以从这里得到它 - :

CommenterprofileImage.heightAnchor.constraint(equalToConstant: 70).isActive = true

Height = 70 you have.

CommenterprofileImage.topAnchor.constraint(equalTo: self.topAnchor,constant:10).isActive = true

commentText.topAnchor.constraint(equalTo: CommenterprofileImage.bottomAnchor,constant:20).isActive

Top Constraint = 10

Top Constraint = 20

因此,您现在需要添加此内容,因为您可以看到我的答案。

<强> - 备注:

  

如果它高于标签,则只添加ImageView高度,否则不需要   为标签上方的内容添加高度和y填充。

     

与计算标签子标题前导或尾随的精确宽度相同   空间。

如果标签覆盖完整宽度(无需减去插入物) - :

func estimatedFrameHeight(text:String) -> CGRect{
        let size = CGSize(width: (view.frame.width), height: 1000)
        let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
        let attribute = [NSFontAttributeName:UIFont.systemFont(ofSize: 11)]
        return NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil)
    }

如果标签距离领先20分,则减去 - :

func estimatedFrameHeight(text:String) -> CGRect{
            let size = CGSize(width: (view.frame.width - 20), height: 1000)
            let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
            let attribute = [NSFontAttributeName:UIFont.systemFont(ofSize: 11)]
            return NSString(string: text).boundingRect(with: size, options: options, attributes: attribute, context: nil)
        }
相关问题