CollecitonViewCell Dynamic With based on Autolayout label Text

584 Views Asked by At

I Have a Horizontal Collection View with Cells containing a label that has auto layout that expands on the size of the cell view.

Im using Xcode 8 and Swift 3.

How can I make so that my cell size is dynamic based on the text? I mean, I want it to expand, so that Otro Text... Reads complete instead of how its shown right now with the dots.

As You can see on the screenshot, now my large text gets trimmed (The Collection view, is on the area that has the Texts: Todo, Otro Text... Test1).

enter image description here

Hope someone can help or orient me on finding a solution.

2

There are 2 best solutions below

2
On BEST ANSWER

You need to make sure that the you add leading and trailing constraint to the label and do not add any width constraint. Next step is to give an estimatedSize in you collectionViewLayout. Example

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.estimatedItemSize = CGSize(
                width: 100, height: collectionView.bounds.size.height)
        }
1
On

You need to set the estimatedItemSize in your UICollectionViewLayout to be able to enable self-sizing cells. It is not possible from Storyboards, only available through code.