Last active
September 22, 2017 20:52
-
-
Save Natata/bb6edf7b09c8a5ddac481dfb5e2ff873 to your computer and use it in GitHub Desktop.
UILabel wrapper can set padding at utility panel.
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
// 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