Question: I am trying to create `UICollectionView` *Horizontal scrolling* (like a top menu single row) with `cell label`. Here, I need to implement my label content different length so based on different length cell label should auto adjust Its width. I am using storyboard for my design. 

**Below my code** 

```
extension CameraViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Constants.reuseID, for: indexPath) as! cameraModeCollectionViewCell
        cell.cameraMode.text = self.items[indexPath.item]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("You selected cell #\(indexPath.item)!")
    }

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {        
        return UIEdgeInsetsMake(0, 40, 0, 40)
   }
}

```

[Current output Iam getting](https://i.stack.imgur.com/qxapL.png)

Answer: 
Implement `UICollectionViewDelegateFlowLayout's` method `collectionView(_:layout:sizeForItemAt)` and calculate the `width` of `collectionViewCell` based the `width` of `text` that you're populating the `label` with.

```
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let text = self.items[indexPath.row]
    let cellWidth = text.size(withAttributes:[.font: UIFont.systemFont(ofSize: 14.0)]).width + 10.0
    return CGSize(width: cellWidth, height: collectionView.bounds.height)
}

```

In the above code I've used `self.items[indexPath.row]` as the source of `text`. `10.0` is the extra `padding` added to the cell.

In case the `items` `array` is `optional`, do `unwrap` the value of `self.items[indexPath.row]` before using.
Step 1 : Find the width of the text that has to be placed in each cell. This can be found using below function
`string.size(withAttributes:[.font: UIFont.systemFont(ofSize: 14.0)]).width`

Using this function we can find width of dynamic text during run time. 

Step 2: Set the size of each cell using the width that we found in Step 1. Size of each cell is set using below delegate 
`func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize`

Combining Step 1 and 2, we can get a collection view which has cell width as per text width that is placed inside it. 

```
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let text = self.items[indexPath.row] // getting text of each index
    let cellWidth = text.size(withAttributes:[.font: UIFont.systemFont(ofSize: 14.0)]).width + 10.0 // getting width of each text + extra margin of 10 
    return CGSize(width: cellWidth, height: 40.0) // height as per your requirement
}

```