Last active
November 15, 2016 10:50
-
-
Save mikeMTOL/79fe0bb97d08bc9a5d0bf7fa1458f6fe 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 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