Created
November 1, 2019 07:29
-
-
Save scspijker/7b8e62eb5b526da9bdca8a028f8e18c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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