iOS - UICollectionViewCell 的动态大小宽度

Posted

技术标签:

【中文标题】iOS - UICollectionViewCell 的动态大小宽度【英文标题】:iOS - dynamically size width of a 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

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

【问题讨论】:

【参考方案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)
  

【讨论】:

传递与标签相同的字体名称和字体大小

以上是关于iOS - UICollectionViewCell 的动态大小宽度的主要内容,如果未能解决你的问题,请参考以下文章

ios开发之-- tableview/collectionview获取当前点击的cell

UICollectionView

如何以编程方式创建 UICollectionViewCell

如何从 UICollectionViewCell 访问 UICollectionView 中 Cell 的 indexPath

Xcode 6 beta 4,UIButton 在collectionviewcell 中不负责

{python之IO多路复用} IO模型介绍 阻塞IO(blocking IO) 非阻塞IO(non-blocking IO) 多路复用IO(IO multiplexing) 异步IO