title: UICollectionViewCell labels overlap each other in background color
body: <p>I am new to iOS but can code in Swift 3. I don't know Objective-C at all.</p>
  
  <p>I am learning <code>UICollectionView</code> and I have set of string arrays. When I am setting custom width to cell it doesn't works so well. the label overlaps one another. After that when I use flowLayout then it trims the label. Also the definite number of 5 cells in one row is fix.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
  
  let reuseIdentifier = "cell"
  var items = ["12", "2", "3221", "434", "52342","5646445646454464654646464", "bdvjsd", "adscfaaaw", "How are you?"];
  
  @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
  override func viewDidLoad() {
      super.viewDidLoad()
  
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return self.items.count
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
  
      cell.myLabel.text = self.items[indexPath.item]
      cell.backgroundColor = UIColor.yellow
      let a = (items[indexPath.item]).size(attributes: nil)
      cell.frame.size.width = a.width + 20
  
      return cell
  }
  
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      // handle tap events
      flowLayout.minimumInteritemSpacing = 5.0
      print("You selected cell #\((items[indexPath.item]).size(attributes: nil).width)!")
  
  }
  }
  </code></pre>
  
  <p>Here's my screenshots:</p>
  
  <p><img src="https://i.imgur.com/mmYkMWEm.png" alt="Screen appear on Load"></p>
  
  <p>Finally I want output like every cell appears individually no overlapping and no trimming of label. If the width of label for "How Are you?" is x then cell containing that label must be of width x+20 (its just assumption) and that label or cell shouldn't overlap on any other cell or label</p>
  
  <p><strong>UPDATE 1</strong></p>
  
  <p>Here is the code what if I changed.</p>
  
  <pre><code>class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
  
  let reuseIdentifier = "cell"
  var items = ["12", "2", "3221", "434", "52342","5646445646454464654646464", "bdvjsd", "adscfaaaw", "How are you?"];
  var x:[Double] = []
  var tempItems:[String] = []
  var flag = true
  
  @IBOutlet weak var flowLayout: UICollectionViewFlowLayout!
  override func viewDidLoad() {
      super.viewDidLoad()
  
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
  }
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return self.items.count
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
      let label: UILabel = (cell.viewWithTag(15) as! UILabel)
      label.text = self.items[indexPath.item]
  
      return cell
  }
  
  // collectionview layoutflow method. first this method call then other delagates method call.
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -&gt; CGSize {
  
      let Labell : UILabel = UILabel()
      Labell.text =   self.items[indexPath.item]
      let labelTextWidth =   Labell.intrinsicContentSize.width
      return CGSize(width: labelTextWidth + 12, height: 35)
  
  }
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      // handle tap events
  
      print("You selected cell #\(indexPath.item)!")
  
  }
  }
  </code></pre>
  
  <p>And the output is same:</p>
  
  <p><img src="https://i.imgur.com/AO5jU6fl.png" alt="Output After Update 1"></p>