添加空间/填充到UILabel我有一个UILabel我想在顶部和底部添加空间的地方。在约束中使用最小高度我将其修改为:编辑:要做到这一点,我用过: override func drawTextInRect(rect: CGRect) {
var insets: UIEdgeInsets = UIEdgeInsets(top: 0.0, left: 10.0, bottom: 0.0, right: 10.0)
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}但我要找到一种不同的方法,因为如果我写两行以上的问题是相同的:
3 回答
绝地无双
TA贡献1946条经验 获得超4个赞
斯威夫特3
import UIKitclass PaddingLabel: UILabel { @IBInspectable var topInset: CGFloat = 5.0 @IBInspectable var bottomInset: CGFloat = 5.0 @IBInspectable var leftInset: CGFloat = 5.0 @IBInspectable var rightInset: CGFloat = 5.0 override func drawText(in rect: CGRect) { let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset) super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)) } override var intrinsicContentSize: CGSize { get { var contentSize = super.intrinsicContentSize contentSize.height += topInset + bottomInset contentSize.width += leftInset + rightInset return contentSize } }}
- 3 回答
- 0 关注
- 619 浏览
添加回答
举报
0/150
提交
取消