Skip to content

Instantly share code, notes, and snippets.

@janeshsutharios
Created April 10, 2021 05:59
Show Gist options
  • Save janeshsutharios/c690e9d0cb983ea96bb79193e40d8dc5 to your computer and use it in GitHub Desktop.
Save janeshsutharios/c690e9d0cb983ea96bb79193e40d8dc5 to your computer and use it in GitHub Desktop.
PaddingUILabel.swift
class PaddingLabel: UILabel {
@IBInspectable var topInset: CGFloat = 5.0
@IBInspectable var bottomInset: CGFloat = 5.0
@IBInspectable var leftInset: CGFloat = 7.0
@IBInspectable var rightInset: CGFloat = 7.0
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: rect.inset(by: insets))
}
override var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: size.width + leftInset + rightInset,
height: size.height + topInset + bottomInset)
}
override var bounds: CGRect {
didSet {
// ensures this works within stack views if multi-line
preferredMaxLayoutWidth = bounds.width - (leftInset + rightInset)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment