Skip to content

Instantly share code, notes, and snippets.

@mikeMTOL
Last active November 15, 2016 10:50
Show Gist options
  • Save mikeMTOL/79fe0bb97d08bc9a5d0bf7fa1458f6fe to your computer and use it in GitHub Desktop.
Save mikeMTOL/79fe0bb97d08bc9a5d0bf7fa1458f6fe to your computer and use it in GitHub Desktop.
@IBDesignable
class UIPaddedLabel:UILabel {
@IBInspectable var topPadding:CGFloat = 0 {
didSet {
updatePadding()
}
}
@IBInspectable var leftPadding:CGFloat = 0 {
didSet {
updatePadding()
}
}
@IBInspectable var rightPadding:CGFloat = 0 {
didSet {
updatePadding()
}
}
@IBInspectable var bottomPadding:CGFloat = 0 {
didSet {
updatePadding()
}
}
private func updatePadding() {
contentInset = UIEdgeInsetsMake(topPadding, leftPadding, bottomPadding, rightPadding)
}
var contentInset:UIEdgeInsets = UIEdgeInsetsZero {
didSet {
setNeedsDisplay()
}
}
convenience init(insets:UIEdgeInsets = UIEdgeInsetsZero, text:String? = nil) {
self.init(frame:CGRectZero)
contentInset = insets
self.text = text
}
override func intrinsicContentSize() -> CGSize {
let size = super.intrinsicContentSize()
return CGSize(width: size.width + contentInset.left + contentInset.right, height: size.height + contentInset.top + contentInset.bottom)
}
override func drawTextInRect(rect: CGRect) {
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, contentInset))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment