Created
April 21, 2022 09:29
-
-
Save GemmaDelOlmo/251e6e287817dd95d9ef02d671bc4369 to your computer and use it in GitHub Desktop.
UILabel subclass with configurable insets
This file contains hidden or 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 Foundation | |
| import UIKit | |
| @IBDesignable public class PaddingLabel: UILabel { | |
| @IBInspectable public var topInset: CGFloat = 5.0 | |
| @IBInspectable public var bottomInset: CGFloat = 5.0 | |
| @IBInspectable public var leftInset: CGFloat = 7.0 | |
| @IBInspectable public var rightInset: CGFloat = 7.0 | |
| public override func drawText(in rect: CGRect) { | |
| let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset) | |
| super.drawText(in: rect.inset(by: insets)) | |
| } | |
| public override var intrinsicContentSize: CGSize { | |
| let size = super.intrinsicContentSize | |
| return CGSize(width: size.width + leftInset + rightInset, | |
| height: size.height + topInset + bottomInset) | |
| } | |
| public override var bounds: CGRect { | |
| didSet { | |
| preferredMaxLayoutWidth = bounds.width - (leftInset + rightInset) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment