Created
April 10, 2021 05:59
-
-
Save janeshsutharios/c690e9d0cb983ea96bb79193e40d8dc5 to your computer and use it in GitHub Desktop.
PaddingUILabel.swift
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 { | |
@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