添加空间/填充到UILabel

添加空间/填充到UILabel

我有一个UILabel我想在顶部和底部添加空间的地方。在约束中使用最小高度我将其修改为:

https://img1.mukewang.com/5d638bec0001308e06000062.jpg

编辑:要做到这一点,我用过:

  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))

    }

但我要找到一种不同的方法,因为如果我写两行以上的问题是相同的:

https://img3.mukewang.com/5d638bef00016cd306020080.jpg


汪汪一只猫
浏览 574回答 3
3回答

绝地无双

斯威夫特3import 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      }    }}
打开App,查看更多内容
随时随地看视频慕课网APP