Skip to content

Instantly share code, notes, and snippets.

@louisdh
Forked from mikeMTOL/UIPaddedLabel.swift
Last active December 19, 2016 15:10
Show Gist options
  • Save louisdh/dcec7ca7bebb496665f2148922ee9cec to your computer and use it in GitHub Desktop.
Save louisdh/dcec7ca7bebb496665f2148922ee9cec to your computer and use it in GitHub Desktop.
Swift 3
import Foundation
import UIKit
@IBDesignable
class UIPaddedLabel: UILabel {
@IBInspectable var topPadding: CGFloat = 0 {
didSet {
updatePadding()
}
}
@IBInspectable var leftPadding: CGFloat = 0 {
didSet {
updatePadding()
}
}
@IBInspectable var rightPadding: CGFloat = 0 {
didSet {
updatePadding()
}
}
@IBInspectable var bottomPadding: CGFloat = 0 {
didSet {
updatePadding()
}
}
private func updatePadding() {
contentInset = UIEdgeInsetsMake(topPadding, leftPadding, bottomPadding, rightPadding)
}
var contentInset: UIEdgeInsets = .zero {
didSet {
setNeedsDisplay()
}
}
convenience init(insets: UIEdgeInsets = .zero, text: String? = nil) {
self.init(frame: .zero)
contentInset = insets
self.text = text
}
override var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: size.width + contentInset.left + contentInset.right, height: size.height + contentInset.top + contentInset.bottom)
}
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, contentInset))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment