Skip to content

Instantly share code, notes, and snippets.

@Natata
Last active September 22, 2017 20:52
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 Natata/bb6edf7b09c8a5ddac481dfb5e2ff873 to your computer and use it in GitHub Desktop.
Save Natata/bb6edf7b09c8a5ddac481dfb5e2ff873 to your computer and use it in GitHub Desktop.
UILabel wrapper can set padding at utility panel.
// source: http://www.cnblogs.com/over140/p/4837652.html
class UILabelPadding : UILabel {
private var padding = UIEdgeInsets.zero
@IBInspectable
var paddingLeft: CGFloat {
get { return padding.left }
set { padding.left = newValue }
}
@IBInspectable
var paddingRight: CGFloat {
get { return padding.right }
set { padding.right = newValue }
}
@IBInspectable
var paddingTop: CGFloat {
get { return padding.top }
set { padding.top = newValue }
}
@IBInspectable
var paddingBottom: CGFloat {
get { return padding.bottom }
set { padding.bottom = newValue }
}
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insets = self.padding
var rect = super.textRect(forBounds: UIEdgeInsetsInsetRect(bounds, insets), limitedToNumberOfLines: numberOfLines)
rect.origin.x -= insets.left
rect.origin.y -= insets.top
rect.size.width += (insets.left + insets.right)
rect.size.height += (insets.top + insets.bottom)
return rect
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment