Skip to content

Instantly share code, notes, and snippets.

@nanoxd
Last active September 22, 2018 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanoxd/07b6f023c8036fdf92dfa0e380f5e981 to your computer and use it in GitHub Desktop.
Save nanoxd/07b6f023c8036fdf92dfa0e380f5e981 to your computer and use it in GitHub Desktop.
[UIInsetLabel] A custom UILabel that can be inset #swift #uikit
/// An UILabel that allows insetting the label
class UIInsetLabel: UILabel {
/// The insets to inset the main `rect` by
var insets: UIEdgeInsets
init(insets: UIEdgeInsets) {
self.insets = insets
super.init(frame: .zero)
}
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insetRect = UIEdgeInsetsInsetRect(bounds, insets)
let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
let invertedInsets = UIEdgeInsets(top: -insets.top, left: -insets.left, bottom: -insets.bottom, right: -insets.right)
return UIEdgeInsetsInsetRect(textRect, invertedInsets)
}
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment