As you can see from the picture, the padding at the top and bottom of the UIButtonLabel is strange.
I want the text to always be centered as in UILabel. Here is my code:
extension UILabel {
func apply(lineHeight: CGFloat, letterSpacing: CGFloat = 0) {
let attributedString = NSMutableAttributedString(string: text ?? " ")
let range = NSMakeRange(0, attributedString.length)
// Line Height
let style = NSMutableParagraphStyle()
style.minimumLineHeight = lineHeight
style.maximumLineHeight = lineHeight
attributedString.addAttribute(.paragraphStyle, value: style, range: range)
attributedString.addAttribute(.baselineOffset, value: (lineHeight - font.lineHeight) / 2, range: range)
// Letter Spacing
attributedString.addAttribute(.kern, value: letterSpacing, range: range)
attributedText = attributedString
}
}
label.apply(lineHeight: 24)
button.titleLabel.apply(lineHeight: 24)