0

I am just trying to dynamically size cells. I followed a few SO Questions and cannot seem to produce the same results. Example one Example two

My code right now is:

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

In the second line, tagName is not identified by Xcode. It is part of my custom cell class. The first SO question I linked somehow was able to identify their cell properties within sizeForItemAt. How would I do that to dynamically size my cells? I do have estimated size to non-nil:

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

My desired result in similar to the photos. Let me know if you need more info. Thank you.

enter image description here enter image description here

2 Answers 2

0

Try with This Way. it's working fine for me in Swift5.

First Implement Collectionview FlowLayout Delegate

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

Add One String Extension.

extension String {
    
    func SizeOf(_ font: UIFont) -> CGSize {
        return self.size(withAttributes: [NSAttributedString.Key.font: font])
    }
    
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

pass same font name and font size you are using for your label

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.