title: UITextView's boundingRect Not Working Properly
body: <p>I'm currently have the following extension on UITextField to calculate the bounding rect for a given string.</p>
  
  <pre><code>func widthHeight(font: UIFont) -&gt; CGRect {
      let constraintRect = CGSize(width: 200, height: 1000)
      let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
      return boundingBox
  }
  </code></pre>
  
  <p>The width for <code>constraintRect</code> is the maximum width I want to allow for the box.</p>
  
  <p>I set the values and the cells like this:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuse, for: indexPath) as? ChatCollectionViewCell {
  
          let text = self.chatLog[indexPath.row].text
          cell.chatTextView.text = text
  
          cell.chatViewWidth = (text?.widthHeight(font: UIFont.systemFont(ofSize: 16)).width)!
  
          return cell
      }
      return UICollectionViewCell()
  }
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
      if let text = self.chatLog[indexPath.row].text {            
          let box = text.widthHeight(font: UIFont.systemFont(ofSize: 16))
          return CGSize(width: view.frame.width, height: box.height + 10)
      }
      return CGSize(width: self.view.frame.width, height: 60)
  }
  </code></pre>
  
  <p>When this code runs, I get massively miscalculated cell sizes:
  <a href="https://i.stack.imgur.com/Yi9XV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yi9XV.png" alt="enter image description here"></a></p>
  
  <p>As you can see, the view's frames are very messed up.
  The first line is "Heya", the second line is "How's life going so far", and the third line is "I'm a stapler, you're a textbook." Some cells are too narrow, some cells are too wide.</p>
  
  <p>Here's some additional code for my custom collectionViewCell:</p>
  
  <pre><code>override init(frame: CGRect) {
      super.init(frame: frame)
  
      setupViews()
  }
  
  override func layoutSubviews() {
  
      chatView.frame = CGRect(x: 0, y: 0, width: chatViewWidth, height: frame.height)
      chatTextView.frame = CGRect(x: chatView.frame.origin.x + 10, y: 0, width: chatView.frame.width - 20, height: chatView.frame.height)
  }
  
  func setupViews() {    
  
      if isTextFromCurrentUser {
          chatTextView.frame = CGRect(x: 10, y: 0, width: frame.width - 140, height: frame.height)
          chatTextView.backgroundColor = .white
      } else {
          chatTextView.frame = CGRect(x: frame.width - 150, y: 0, width: frame.width - 140, height: frame.height)
          chatTextView.backgroundColor = .blue
      }
  
      chatTextView.font = UIFont.systemFont(ofSize: 16)
      chatTextView.layer.cornerRadius = 9
      chatTextView.clipsToBounds = true
      chatTextView.autoresizingMask = UIViewAutoresizing.flexibleHeight
      chatTextView.isScrollEnabled = false
  
      contentView.addSubview(chatView)
      contentView.addSubview(chatTextView)
  }
  </code></pre>