Created
November 13, 2025 09:05
-
-
Save dterekhov/65d57b78dd75d339ecd7f45bc825133f to your computer and use it in GitHub Desktop.
PaddingLabel: Simple adding insest for UILabel #uikit #swift-api #real-project
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 UIKit | |
| @IBDesignable public class PaddingLabel: UILabel { | |
| // MARK: - Members | |
| @IBInspectable public var topInset: CGFloat = 0.0 | |
| @IBInspectable public var bottomInset: CGFloat = 0.0 | |
| @IBInspectable public var leftInset: CGFloat = 0.0 | |
| @IBInspectable public var rightInset: CGFloat = 0.0 | |
| // MARK: - Lifecycle | |
| override public func drawText(in rect: CGRect) { | |
| let insets = UIEdgeInsets.init(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset) | |
| super.drawText(in: rect.inset(by: insets)) | |
| } | |
| override public var intrinsicContentSize: CGSize { | |
| let size = super.intrinsicContentSize | |
| return CGSize(width: size.width + leftInset + rightInset, | |
| height: size.height + topInset + bottomInset) | |
| } | |
| // MARK: - Setup | |
| public func setup(insets: UIEdgeInsets) { | |
| topInset = insets.top | |
| bottomInset = insets.bottom | |
| leftInset = insets.left | |
| rightInset = insets.right | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment