自定义UILabel类中的角半径

时间:2017-04-19 08:43:22

标签: ios swift uikit

我在Storyboard中有一个标签,我用以下代码

对自定义类进行子类化
@IBDesignable class PaddingLabel: UILabel {

    @IBInspectable var topInset: CGFloat = 5.0
    @IBInspectable var bottomInset: CGFloat = 5.0
    @IBInspectable var leftInset: CGFloat = 7.0
    @IBInspectable var rightInset: CGFloat = 7.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 {
        var intrinsicSuperViewContentSize = super.intrinsicContentSize
        intrinsicSuperViewContentSize.height += topInset + bottomInset
        intrinsicSuperViewContentSize.width += leftInset + rightInset
        return intrinsicSuperViewContentSize
    }
}

我在自定义的TableviewCell label?.layer.cornerRadius = 5函数中添加了awakeFromNib

它似乎没有影响标签。

1 个答案:

答案 0 :(得分:6)

添加clipsToBounds属性:

label?.clipsToBounds = true // or use label?.layer.masksToBounds = true