Skip to content

Instantly share code, notes, and snippets.

@Mildwhale
Created June 4, 2020 01:46
Show Gist options
  • Save Mildwhale/f21afced6044c789e970cf15ff2d147b to your computer and use it in GitHub Desktop.
Save Mildwhale/f21afced6044c789e970cf15ff2d147b to your computer and use it in GitHub Desktop.
UILabel with padding.
class PaddingLabel: UILabel {
private let inset: UIEdgeInsets
init(inset: UIEdgeInsets = .zero) {
self.inset = inset
super.init(frame: .zero)
}
required init?(coder: NSCoder) {
fatalError()
}
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: inset))
}
override var intrinsicContentSize: CGSize {
var intrinsicContentSize = super.intrinsicContentSize
intrinsicContentSize.width += inset.left + inset.right
intrinsicContentSize.height += inset.top + inset.bottom
return intrinsicContentSize
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment