Created
June 4, 2020 01:46
-
-
Save Mildwhale/f21afced6044c789e970cf15ff2d147b to your computer and use it in GitHub Desktop.
UILabel with padding.
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 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