Last active
November 6, 2017 14:26
-
-
Save dimitris-c/8d32a81fe711f17665f113fce78d7c15 to your computer and use it in GitHub Desktop.
A UILabel subclass that provides extra padding for background
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
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