9

I'm trying to set some insets in a UILabel. It worked perfectly, but now UIEdgeInsetsInsetRect has been replaced with CGRect.inset(by:) and I can't find out how to solve this.

When I'm trying to use CGRect.inset(by:) with my insets, then I'm getting the message that UIEdgeInsets isn't convertible to CGRect.

My code

class TagLabel: UILabel {
    
    override func draw(_ rect: CGRect) {
        let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
        
        super.drawText(in: CGRect.insetBy(inset))
//        super.drawText(in: UIEdgeInsetsInsetRect(rect, inset)) // Old code
    }

}

Anyone knows how to set the insets to the UILabel?

4 Answers 4

17

Imho you also have to update the intrinsicContentSize:

class InsetLabel: UILabel {

    let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

    override func drawText(in rect: CGRect) {
        super.drawText(in: rect.inset(by: inset))
    }

    override var intrinsicContentSize: CGSize {
        var intrinsicContentSize = super.intrinsicContentSize
        intrinsicContentSize.width += inset.left + inset.right
        intrinsicContentSize.height += inset.top + inset.bottom
        return intrinsicContentSize
    }

}
1
  • 3
    This is the better answer. If you're finicky enough to be messing with the insets, you're also going to want sizeToFit(). The intrinsicContentSize method above lets that work correctly.
    – dar512
    Mar 25, 2020 at 23:16
15

Please update your code as below

 class TagLabel: UILabel {

    override func draw(_ rect: CGRect) {
        let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
        super.drawText(in: rect.insetBy(inset))
    }
}
3
  • I used rect.inset(inset) -> which takes UIEdgeInsets Jun 26, 2018 at 9:44
  • @JacobAhlberg, sorry, there are 2 methods :)
    – Eduard
    Jun 26, 2018 at 9:45
  • In Swift 4 (Apr. 11, 2020), I had to use: super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)) instead of: super.drawText(in: rect.inset(by: insets)) Apr 12, 2021 at 0:39
4

this code differs from the accepted answer because the accepted answer uses insetBy(inset) and this answer uses inset(by: inset). When I added this answer in iOS 10.1 and Swift 4.2.1 autocomplete DID NOT give you rect.inset(by: ) and I had to manually type it in. Maybe it does in Swift 5, I'm not sure

For iOS 10.1 and Swift 4.2.1 use rect.inset(by:)

this:

override func draw(_ rect: CGRect) {

    let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)

    super.drawText(in: rect.inset(by: inset))
}
0
0

Swift 5 replace method drawText(...) with draw(...)

extension UILabel {

open override func draw(_ rect: CGRect) {
    let inset = UIEdgeInsets(top: -2, left: 2, bottom: -2, right: 2)
    super.draw(rect.inset(by: inset))
    
}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.