Skip to content

Instantly share code, notes, and snippets.

@GemmaDelOlmo
Created April 21, 2022 09:29
Show Gist options
  • Select an option

  • Save GemmaDelOlmo/251e6e287817dd95d9ef02d671bc4369 to your computer and use it in GitHub Desktop.

Select an option

Save GemmaDelOlmo/251e6e287817dd95d9ef02d671bc4369 to your computer and use it in GitHub Desktop.
UILabel subclass with configurable insets
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