OGeek|极客世界-中国程序员成长平台

标题: ios - Swift 给 uicollectionviewcell 宽度和动态高度 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:29
标题: ios - Swift 给 uicollectionviewcell 宽度和动态高度

我有这个 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。



Best Answer-推荐答案


你可以这样做-:

先用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)

    }

让高度 =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

顶部约束 = 10

顶部约束 = 20

所以你现在需要添加这个,正如你在回答中看到的那样。

备注-:

only add ImageView Height if it is above label else not require, just add height and y padding for what ever you have above label.

Same to calculate exact width for label subtarct leading or trailing space.

如果标签覆盖整个宽度(不需要插入减法)-:

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)
        }

关于ios - Swift 给 uicollectionviewcell 宽度和动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45663870/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4