简体   繁体   中英

Add Padding to UILabel

I want to add left padding to text in a UILabel programmatically. I've searched for a solution but nothing that I can get to work within my existing UILabel extension.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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