【问题标题】:iOS - dynamically size width of a UICollectionViewCelliOS - UICollectionViewCell 的动态大小宽度
【发布时间】:2020-08-18 02:48:41
【问题描述】:

我只是想动态调整单元格的大小。我遵循了一些 SO Questions 并且似乎无法产生相同的结果。 Example one Example two

我现在的代码是:

extension ItemViewController: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let text = allTags[indexPath.item]
        tagName.text = text //label from my custom cell (xCode not recognizing)
        return CGRect(width: tagName.intrinsicContentSize.width + 60, height: 40)
}
}

在第二行中,Xcode 无法识别 tagName。它是我的自定义单元类的一部分。我以某种方式链接的第一个 SO 问题能够在 sizeForItemAt 中识别它们的单元格属性。我将如何做到这一点来动态调整我的单元格的大小?我确实估计大小不为零:

if let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            collectionViewLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}

我想要的结果与照片相似。如果您需要更多信息,请告诉我。谢谢。

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

    尝试这种方式。 它在 Swift5 中对我来说工作正常。

    首先实现Collectionview FlowLayout 委托

    extension ItemViewController: UICollectionViewDelegateFlowLayout {
        
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            
            let text = "Your Lable Text"
            let font: UIFont = UIFont(name: "Font Name", size: 20) ??  UIFont.systemFont(ofSize: 17.0) // set here font name and font size
            let width = text.SizeOf(font).width
            return CGSize(width: width + 20.0 , height: 40.0) // ADD width + space between text (for ex : 20.0)
            }    
    }
    

    添加一个字符串扩展名

    extension String {
        
        func SizeOf(_ font: UIFont) -> CGSize {
            return self.size(withAttributes: [NSAttributedString.Key.font: font])
        }
        
    }
    

    【讨论】:

      【解决方案2】:
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
              let width = UILabel.textWidth(font: UIFont(name: "pass same font name used in label", size: 12.0) ?? UIFont.systemFont(ofSize:
      12.0), text: dataArray(indexPath.item))
              return CGSize(width: width + 20, height: 50)
          }
      
      extension UILabel {
      class func textWidth(font: UIFont, text: String) -> CGFloat {
            let myText = text as NSString
            let rect = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
            let labelSize = myText.boundingRect(with: rect, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
            return ceil(labelSize.width)
        }
      }
      

      【讨论】:

      • 传递与标签相同的字体名称和字体大小
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      相关资源
      最近更新 更多