• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - Swift 给 uicollectionviewcell 宽度和动态高度

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:29:58 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap