title: How to center UILabel text top and bottom spaces
body: <p>I have a <code>UILabel</code> in a stackview in a content view that is in a tableview cell. Everything is working correctly and the cell is resizing but i cannot center the text in the <code>UILabel</code>. I always have strange spaces in the bottom of the text. Check out screen shot.</p>
  <p>Yellow: content view
  Green: stackview
  Blue: label</p>
  <p><strong>Additional info:</strong> No static height for cells. The yellow view has top bottom and trailing and leading constraints plus horizontal and vertical aligned to super view same thing the stack view. The only thing that has constant height which I activate and deactivate through code is the button. So it hides and shows according to the cell. Stackview is fill proportionally.</p>
  <p>The <code>UILabel</code> is <code>sizeToFit</code></p>
  <p>Also tried:</p>
  <pre><code>detailLbl.textAlignment = .center
  detailLbl.lineBreakMode = .byWordWrapping //also tried byClipping
  detailLbl.baselineAdjustment = .alignCenters
  </code></pre>
  <p>The text inside i parse as <strong>HTML</strong></p>
  <pre><code>&quot;&lt;p&gt;&lt;font color=\&quot;#cd1719\&quot;&gt;V/. &lt;/font&gt;…The Word of the Lord.&lt;br&gt;&lt;b&gt;&lt;/b&gt;&lt;font color=\&quot;#cd1719\&quot;&gt;R/. &lt;/font&gt;&lt;b&gt;Thanks be to God.&lt;/b&gt;&lt;/p&gt;&quot;
  </code></pre>
  <p>Second screenshot</p>
  <pre><code>&quot;&lt;h6 lang=\&quot;en\&quot; style=\&quot;color=#cd1719 !important; font-weight: normal !important\&quot;&gt;&lt;font color=\&quot;#cd1719\&quot; weight=\&quot;normal\&quot;&gt;&lt;i&gt;The alleluia is said or sung.&lt;/i&gt;&lt;/font&gt;&lt;/h6&gt;&quot;
  </code></pre>
  <p>This is an <code>NSAttributedString</code> extension I use for parsing HTML</p>
  <pre><code> extension NSAttributedString {
  
      convenience init(htmlString html: String, font: UIFont? = nil, useDocumentFontSize: Bool = true) throws {
          let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [
              .documentType: NSAttributedString.DocumentType.html,
              .characterEncoding: String.Encoding.utf8.rawValue
          ]
  
          let data = html.data(using: .utf8, allowLossyConversion: true)
          guard (data != nil), let fontFamily = font?.familyName, let attr = try? NSMutableAttributedString(data: data!, options: options, documentAttributes: nil) else {
              try self.init(data: data ?? Data(html.utf8), options: options, documentAttributes: nil)
              return
          }
  
          let fontSize: CGFloat? = useDocumentFontSize ? nil : font!.pointSize
          let range = NSRange(location: 0, length: attr.length)
          attr.enumerateAttribute(.font, in: range, options: .longestEffectiveRangeNotRequired) { attrib, range, _ in
              if let htmlFont = attrib as? UIFont {
                  let traits = htmlFont.fontDescriptor.symbolicTraits
                  var descrip = htmlFont.fontDescriptor.withFamily(fontFamily)
  
                  if (traits.rawValue &amp; UIFontDescriptor.SymbolicTraits.traitBold.rawValue) != 0 {
                      descrip = descrip.withSymbolicTraits(.traitBold)!
                  }
  
                  if (traits.rawValue &amp; UIFontDescriptor.SymbolicTraits.traitItalic.rawValue) != 0 {
                      descrip = descrip.withSymbolicTraits(.traitItalic)!
                  }
                  
                  attr.addAttribute(.font, value: UIFont(descriptor: descrip, size: fontSize ?? htmlFont.pointSize), range: range)
              }
          }
  
          self.init(attributedString: attr)
      }
  
  }
  </code></pre>
  <p>And this is how I use it in the code</p>
  <pre><code>if let attributedString = try? NSAttributedString(htmlString: htmlTxt, font: UIFont(name: &quot;Avenir-Roman&quot;, size: CGFloat(Constants.defualtContentDescriptionFontSize)), useDocumentFontSize: false) {
          cell.detailLbl.attributedText = attributedString
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/dyY50.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dyY50.jpg" alt="enter image description here" /></a>
  <a href="https://i.stack.imgur.com/U5nbI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U5nbI.jpg" alt="enter image description here" /></a></p>