title: Swift How to wrap long text of label in collection cell and set margin in text label
body: <p>For now, I am using custom collection view layout to implement this.</p>
  
  <p><a href="https://i.stack.imgur.com/lj4Ec.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lj4Ec.png" alt="enter image description here"></a></p>
  
  <p>Each cell contains a label with different length of text. I return the width of cell by using the function below:</p>
  
  <pre><code>func collectionView(collectionView: UICollectionView, widthForSubsectionAtIndexPath indexPath: NSIndexPath, height: CGFloat) -&gt; CGFloat {
      let text = subsections[indexPath.row]
      let font = UIFont(name: "Helvetica neue", size: 24)
      dLog("height ==&gt; \(height)")
      let rect = NSString(string: text).boundingRectWithSize(CGSize(width: CGFloat(MAXFLOAT), height: height), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font!], context: nil)
      dLog("rect ==&gt; \(rect)")
      return rect.width
  }
  </code></pre>
  
  <p>This can wrap text just as shown in picture. But I also want to make the distance between text is the same. The red circles above are not in same size. I guess it depends on the length of text.</p>
  
  <p>Here is the constrain that I am using:
  <a href="https://i.stack.imgur.com/U6cqT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U6cqT.png" alt="enter image description here"></a></p>
  
  <p>How can I set margin distance between text and cell same and at same time wrap words correctly?</p>
  
  <p>BTW, When I output the <code>height</code> and <code>rect</code> information in console, I find the height information is different. The height of grey bar is actually 50. Here is the output:</p>
  
  <blockquote>
    <p>[SubsectionHeaderCollectionViewController.swift:91] collectionView(<em>:widthForSubsectionAtIndexPath:height:) - height ==> 50.0
    2016-06-24 12:13:50.507 Atlas[12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(</em>:widthForSubsectionAtIndexPath:height:) - rect ==> (0.0, 0.0, 63.192, 27.96)
    2016-06-24 12:13:50.507 Atlas[12714:496386] [SubsectionHeaderCollectionViewController.swift:91] collectionView(<em>:widthForSubsectionAtIndexPath:height:) - height ==> 50.0
    2016-06-24 12:13:50.507 Atlas[12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(</em>:widthForSubsectionAtIndexPath:height:) - rect ==> (0.0, 0.0, 60.432, 27.96)
    2016-06-24 12:13:50.507 Atlas[12714:496386] [SubsectionHeaderCollectionViewController.swift:91] collectionView(<em>:widthForSubsectionAtIndexPath:height:) - height ==> 50.0
    2016-06-24 12:13:50.508 Atlas[12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(</em>:widthForSubsectionAtIndexPath:height:) - rect ==> (0.0, 0.0, 241.8, 27.96)
    2016-06-24 12:13:50.508 Atlas[12714:496386] [SubsectionHeaderCollectionViewController.swift:91] collectionView(<em>:widthForSubsectionAtIndexPath:height:) - height ==> 50.0
    2016-06-24 12:13:50.508 Atlas[12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(</em>:widthForSubsectionAtIndexPath:height:) - rect ==> (0.0, 0.0, 98.232, 27.96)
    2016-06-24 12:13:50.508 Atlas[12714:496386] [SubsectionHeaderCollectionViewController.swift:91] collectionView(<em>:widthForSubsectionAtIndexPath:height:) - height ==> 50.0
    2016-06-24 12:13:50.508 Atlas[12714:496386] [SubsectionHeaderCollectionViewController.swift:93] collectionView(</em>:widthForSubsectionAtIndexPath:height:) - rect ==> (0.0, 0.0, 56.88, 27.96)</p>
  </blockquote>