title: Self-sizing UILabel with UIView Subclassing
body: <p>I have a sketch and I'm trying to achieve the concept programmatically.</p>
  
  <p>Here's my Sketch: </p>
  
  <p><a href="https://i.stack.imgur.com/N3Ezn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N3Ezn.png" alt="enter image description here"></a></p>
  
  <p>Achievement:
  <a href="https://i.stack.imgur.com/tTB2z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tTB2z.png" alt="enter image description here"></a></p>
  
  <p>My code: </p>
  
  <pre><code>let luna = UIView()
  
  let descStrWidth = messageDescription.stringWidth  // Using String Extension i made.
  let descStrHeight = messageDescription.stringHeight // Using String Extension i made.
  
  let titleStrHeight = messageTitle.stringHeight // ..
  let titleStrWidth = messageTitle.stringWidth // .. 
  
  
  let titleLabel = UILabel()
  titleLabel.text = messageTitle
  titleLabel.font = UIFont(name: "avenirnext-demibold", size: 13)
  titleLabel.textColor = UIColor(hexString: "4A4A4A")
  titleLabel.numberOfLines = 0 // whole idea of self-sizing 
  
  let titleDesc = UILabel()
  titleDesc.text = messageDescription
  titleDesc.font = UIFont(name: "avenirnext-regular", size: 9)
  titleDesc.textColor = UIColor(hexString: "4A4A4A")
  titleDesc.numberOfLines = 0
  
  // My mainView 
  
  luna.frame = CGRect(x: 16, y: 40, width: screenWidth - 30, height: titleStrHeight + descStrHeight)
  luna.center.x = luna.center.x
  luna.backgroundColor = .white
  luna.addShadow(radius: 11, opacity: 0.2) // Some Shadow
  luna.layer.cornerRadius = 10
  
  titleDesc.frame = CGRect(x: luna.frame.minX + 3, y: titleLabel.frame.maxY + titleStrHeight, width: luna.frame.width, height: descStrHeight)
  
  
  titleLabel.frame = CGRect(x: luna.frame.minX + 3, y: 8, width: titleStrWidth, height: titleStrHeight)
  
  luna.addSubview(titleLabel)
  luna.addSubview(titleDesc)
  self.view.addSubview(luna)
  </code></pre>
  
  <p>Getting provided string width and height:</p>
  
  <pre><code> extension String {
      var stringWidth: CGFloat {
          let constraintRect = CGSize(width: UIScreen.main.bounds.width, height: .greatestFiniteMagnitude)
          let boundingBox = self.trimmingCharacters(in: .whitespacesAndNewlines).boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)], context: nil)
          return boundingBox.width
      }
  
      var stringHeight: CGFloat {
          let constraintRect = CGSize(width: UIScreen.main.bounds.width, height: .greatestFiniteMagnitude)
          let boundingBox = self.trimmingCharacters(in: .whitespacesAndNewlines).boundingRect(with: constraintRect, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)], context: nil)
          return boundingBox.height
      }
  }
  </code></pre>
  
  <p>This is what I achieved so far. This might be easier with constraints, right?</p>
  
  <p><a href="https://i.stack.imgur.com/5LD4f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5LD4f.png" alt="HELP"></a></p>