Skip to content

Instantly share code, notes, and snippets.

@ozgur
Created May 19, 2017 20:17
Show Gist options
  • Save ozgur/5de69513a4f16a694c589affc30c49ad to your computer and use it in GitHub Desktop.
Save ozgur/5de69513a4f16a694c589affc30c49ad to your computer and use it in GitHub Desktop.
A UILabel subclass that supports insetting the content.
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