Created
May 19, 2017 20:17
-
-
Save ozgur/5de69513a4f16a694c589affc30c49ad to your computer and use it in GitHub Desktop.
A UILabel subclass that supports insetting the content.
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
import UIKit | |
class Label: UILabel { | |
var contentInset: UIEdgeInsets = .zero { | |
didSet { | |
invalidateIntrinsicContentSize() | |
setNeedsLayout() | |
} | |
} | |
override func drawText(in rect: CGRect) { | |
super.drawText(in: UIEdgeInsetsInsetRect(rect, contentInset)) | |
} | |
override var intrinsicContentSize: CGSize { | |
var size = super.intrinsicContentSize | |
size.height += contentInset.top + contentInset.bottom | |
size.width += contentInset.left + contentInset.right | |
return size | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment