简体   繁体   English

UICollectionView中的动态宽度单元格

[英]Dynamic Width Cell in UICollectionView

在此处输入图片说明

I try to implement labels with UICollectionView . 我尝试使用UICollectionView实现标签。 But I have to set the widths with dynamic labels length. 但是我必须使用动态标签的长度来设置宽度。 I try to remove gaps in picture. 我试图消除图片上的空白。 I share some code. 我分享一些代码。 I use Nibs. 我用鸟嘴。

Thanks in advance. 提前致谢。

layoutFCV.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
let filterCV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layoutFCV)

-- -

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {       
    if collectionView == filterCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterSelectionCollectionViewCell", for: indexPath) as! FilterSelectionCollectionViewCell
        return CGSize(width: cell.title.frame.width , height: self.filterCollectionView.frame.height)
    }  else {
        return CGSize(width: 10, height: 10)
    }
}
extension ViewController: UICollectionViewDelegateFlowLayout {
     func collectionView(_ collectionView: UICollectionView, layout      collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
     return "String".size(withAttributes: [
       NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 14)
  ])
  }
 }

Use this code: 使用此代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let text = "Title"
    let width = self.estimatedFrame(text: text, font: UIFont.systemFont(ofSize: 14)).width
    return CGSize(width: width, height: 50.0)
}

func estimatedFrame(text: String, font: UIFont) -> CGRect {
    let size = CGSize(width: 200, height: 1000) // temporary size
    let options = NSStringDrawingOptions.usesFontLeading.union(.usesLineFragmentOrigin)
    return NSString(string: text).boundingRect(with: size,
                                               options: options,
                                               attributes: [NSFontAttributeName: font],
                                               context: nil)
}

First make sure your constraints in UICollectionViewCell.xib file has been set correctly. 首先,确保已正确设置UICollectionViewCell.xib文件中的约束。 I mean with the growth of UILabel in cell, the cell itself should grow as well. 我的意思是,随着单元格中UILabel的增长,单元格本身也应增长。

You should explicitly set the title before you get the cell size. 在获取像元大小之前,应明确设置标题。 So here is your collectionView(collectionView:, layout:, sizeForItemAt:) method should be: 所以这是您的collectionView(collectionView:, layout:, sizeForItemAt:)方法应为:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {       
    if collectionView == filterCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "FilterSelectionCollectionViewCell", for: indexPath) as! FilterSelectionCollectionViewCell
        cell.title.text = "Newest" //this is for the "Newest" cell. Of curse you should set the proper title for each indexPath
        cell.setNeedsLayout()
        cell.layoutIfNeede()
        return CGSize(width: cell.contenView.frame.width , height: cell.contentView.frame.height)
    }  else {
        return CGSize(width: 10, height: 10)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM