Skip to content

Instantly share code, notes, and snippets.

@Ariandr
Created July 18, 2018 11:37
Show Gist options
  • Select an option

  • Save Ariandr/cc54d1844bfc2af0cc592e25a22952af to your computer and use it in GitHub Desktop.

Select an option

Save Ariandr/cc54d1844bfc2af0cc592e25a22952af to your computer and use it in GitHub Desktop.
import UIKit
class InsetLabel: UILabel {
private let topInset: CGFloat
private let leftInset: CGFloat
private let bottomInset: CGFloat
private let rightInset: CGFloat
init(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) {
self.topInset = top
self.leftInset = left
self.bottomInset = bottom
self.rightInset = right
super.init(frame: .zero)
}
convenience override init(frame: CGRect) {
self.init(top: 0, left: 0, bottom: 0, right: 0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var insets: UIEdgeInsets {
return UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
}
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
var adjSize = super.sizeThatFits(size)
adjSize.width += leftInset + rightInset
adjSize.height += topInset + bottomInset
return adjSize
}
override var intrinsicContentSize: CGSize {
var contentSize = super.intrinsicContentSize
contentSize.width += leftInset + rightInset
contentSize.height += topInset + bottomInset
return contentSize
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment