Skip to content

Instantly share code, notes, and snippets.

@dimitris-c
Last active November 6, 2017 14:26
Show Gist options
  • Save dimitris-c/8d32a81fe711f17665f113fce78d7c15 to your computer and use it in GitHub Desktop.
Save dimitris-c/8d32a81fe711f17665f113fce78d7c15 to your computer and use it in GitHub Desktop.
A UILabel subclass that provides extra padding for background
class EdgesLabel: UILabel {
var edgeInsets: UIEdgeInsets = .zero {
didSet {
layoutIfNeeded()
}
}
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, edgeInsets))
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
return super.textRect(forBounds: UIEdgeInsetsInsetRect(bounds, edgeInsets), limitedToNumberOfLines: numberOfLines)
}
override var intrinsicContentSize: CGSize {
var size = super.intrinsicContentSize
size.width += self.edgeInsets.left + self.edgeInsets.right
size.height += self.edgeInsets.top + self.edgeInsets.bottom
return size
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment