title: How do I create padding around the text of a UILabel?
body: <p>I'm styling a UILabel programmatically and having trouble getting any padding around the label's text:</p>
  
  <p><a href="https://i.stack.imgur.com/YBb4A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YBb4A.png" alt="enter image description here"></a> </p>
  
  <p>After studying many SO threads on the subject (<a href="https://stackoverflow.com/questions/21167226/resizing-a-uilabel-to-accommodate-insets/21267507#21267507">this one</a>, in particular) I thought I'd found a solution but it has no affect on the label. I've created a custom class for the label that subclasses <code>UILabel</code>:</p>
  
  <pre><code>// Swift 3 (I know, I know...)
  
  class PublicationTypeBadge: UILabel {
      var textInsets = UIEdgeInsets.zero {
          didSet { invalidateIntrinsicContentSize() }
      }
  
      override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -&gt; CGRect {
          let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
          let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
          let invertedInsets = UIEdgeInsets(top: -textInsets.top,
                                            left: -textInsets.left,
                                            bottom: -textInsets.bottom,
                                            right: -textInsets.right)
          return UIEdgeInsetsInsetRect(textRect, invertedInsets)
      }
  
      override func drawText(in rect: CGRect) {
          super.drawText(in: UIEdgeInsetsInsetRect(rect, textInsets))
      }
  }
  </code></pre>
  
  <p>And I invoke it like so:</p>
  
  <pre><code>func setPubTypeBadge(publication: PublicationModel, indexPathRow: Int) {
      if let pubType = publication.publicationType,
          let pubEnum = PublicationType(rawValue: pubType) {
          pubTypeBadge.attributedText = pubEnum.getBadgeLabel(using: pubType)
      }
  
      let rect = pubTypeBadge.textRect(forBounds: pubTypeBadge.frame, limitedToNumberOfLines: 1)
      pubTypeBadge.drawText(in: rect)
      pubTypeBadge.layer.borderColor = UIColor.dsShade3.cgColor
      pubTypeBadge.layer.borderWidth = 1
      pubTypeBadge.layer.cornerRadius = 4
  }
  </code></pre>
  
  <p>which does absolutely nothing as far as the padding/insets are concerned. I simplly want 6pts of space on the left/right between the text and the border and 4pts on the top/bottom. Any help greatly appreciated. </p>