简体   繁体   English

将填充添加到UILabel

[英]Add Padding to UILabel

I want to add left padding to text in a UILabel programmatically. 我想以编程方式将左填充添加到UILabel中的文本。 I've searched for a solution but nothing that I can get to work within my existing UILabel extension. 我一直在寻找解决方案,但是在现有的UILabel扩展中无法正常工作。

class TopSideHeaderLabel: UILabel {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        adjustsFontSizeToFitWidth = true
        font = UIFont(name: Theme.PrimaryFontSemiBold, size: 16)
        textColor = Theme.White

        backgroundColor = Theme.background2
        textAlignment = .left
    }
}

What about using insets? 怎样使用插图?

class TopSideHeaderLabel: UILabel {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        adjustsFontSizeToFitWidth = true
        font = UIFont(name: Theme.PrimaryFontSemiBold, size: 16)
        textColor = Theme.White

        backgroundColor = Theme.background2
        textAlignment = .left
    }

    override func drawText(in rect: CGRect) {
        let insets = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
        super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM