添加填充到内在内容大小不适用于UILabel

时间:2016-05-21 18:45:33

标签: ios swift uiview swift2 uilabel

我希望为这些UILabels添加额外的填充。我使用以下代码更改了内在内容大小:

class CustomLabelWithPadding:UILabel{


    override func intrinsicContentSize() -> CGSize {
        let contentSize = super.intrinsicContentSize()
        return CGSize(width: contentSize.width + 50, height: contentSize.height)
    }

}

但是标签没有填充 enter image description here

 let label = CustomLabelWithPadding()
 label.text = "Hello World"
 label.font = UIFont(name: "Helvetica", size: 20)
 label.sizeToFit()
 label.layer.backgroundColor = UIColor(red: 0.067, green: 0.773, blue: 0.525, alpha: 1.00).CGColor
 label.textAlignment = NSTextAlignment.Center
 label.textColor = UIColor.whiteColor()
 label.frame.origin.x = 160
 label.frame.origin.y = 50
 self.view.addSubview(label)

1 个答案:

答案 0 :(得分:0)

这个人完成了这项工作:)

{{1}}