Skip to content

Instantly share code, notes, and snippets.

@scspijker
Created November 1, 2019 07:29
Show Gist options
  • Save scspijker/7b8e62eb5b526da9bdca8a028f8e18c9 to your computer and use it in GitHub Desktop.
Save scspijker/7b8e62eb5b526da9bdca8a028f8e18c9 to your computer and use it in GitHub Desktop.
@IBDesignable
class UILabelWithLineHeight: UILabel {
override var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
let padding = (1.0 - lineHeightMultiple) * font.pointSize
size.height += padding
return size
}
override var text: String? {
didSet {
updateAttributedText()
}
}
@IBInspectable var lineHeightMultiple: CGFloat = 1.0 {
didSet {
updateAttributedText()
}
}
private func updateAttributedText() {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineHeightMultiple = lineHeightMultiple
attributedText = NSAttributedString(string: text ?? "", attributes: [
.font: font,
.paragraphStyle: paragraphStyle,
.foregroundColor: textColor
])
invalidateIntrinsicContentSize()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment