-
-
Save louisdh/dcec7ca7bebb496665f2148922ee9cec to your computer and use it in GitHub Desktop.
Swift 3
This file contains 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 | |
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