1

I have set my UILabel padding using StackOverflow's popular thread for resolving auto-layout issue. This thread is basically a UILabel extension.

Part of the answer is:

class NRLabel : UILabel {

    var textInsets = UIEdgeInsets.zero {
        didSet { invalidateIntrinsicContentSize() }
    }

    override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        let insetRect = UIEdgeInsetsInsetRect(bounds, textInsets)
        let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
        let invertedInsets = UIEdgeInsets(top: -textInsets.top,
                                          left: -textInsets.left,
                                          bottom: -textInsets.bottom,
                                          right: -textInsets.right)
        return UIEdgeInsetsInsetRect(textRect, invertedInsets)
    }

    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, textInsets))
    }
}

Everything is working fine, padding has been added but i need to reload the tableViewcell to see the effect.

I have overridden my customCell's viewWillLayoutSubviews() function like this for padding

 self.chatLabel.textInsets = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)

And the effect is like this.

enter image description here

You see, first label is getting it's padding after reloading the cell.

Pls suggest how to achieve UILabel padding by using the extension mentioned above so that this issue is resolved.

elk_cloner
  • 2,049
  • 1
  • 12
  • 13

4 Answers4

0

Add this two function into your extention and remove all other:

 override func drawText(in rect: CGRect) {
            let insets: UIEdgeInsets = 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
        }
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
0

Using your NRLabel class, this works fine for me. The theLabel is added in Storyboard TableViewCell Prototype, with constraints set to allow auto-sizing rows.

class PaddedLabelCell : UITableViewCell {

    @IBOutlet weak var theLabel: NRLabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.setupLabel()
    }

    func setupLabel() -> Void {
        theLabel.layer.cornerRadius = 8.0
        theLabel.layer.masksToBounds = true
        theLabel.textInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        // any other setup stuff can go here....
    }

}
DonMag
  • 69,424
  • 5
  • 50
  • 86
0

You have nothing in NRLabel which tells it to redraw when the textInsets property is changed. You need to do something like this:

var textInsets = UIEdgeInsets.zero {
    didSet { 
        invalidateIntrinsicContentSize() 
        setNeedsDisplay()
    }
}

So now when the textInsets property is changed the NRLabel will be re-drawn with the new text insets.

Upholder Of Truth
  • 4,643
  • 2
  • 13
  • 23
-1

After some long tiring hours i just found the solution which i never tried and i don't know why. :(

In cellForRowAtIndexPath i just wrote the line which was in customCell's file.

Just call this line before returning cell.

cell.chatLabel.textInsets = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)

The reason behind it may be, Cell is drawn with all the functionality but it didn't get much to refresh it's UI. So calling the line just before showing the cell resolve the problem.

elk_cloner
  • 2,049
  • 1
  • 12
  • 13