Skip to content

Instantly share code, notes, and snippets.

@dterekhov
Created November 13, 2025 09:05
Show Gist options
  • Select an option

  • Save dterekhov/65d57b78dd75d339ecd7f45bc825133f to your computer and use it in GitHub Desktop.

Select an option

Save dterekhov/65d57b78dd75d339ecd7f45bc825133f to your computer and use it in GitHub Desktop.
PaddingLabel: Simple adding insest for UILabel #uikit #swift-api #real-project
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