Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel border and padding

Tags:

ios

swift

I want to add padding inside my label in order to have spaces between it and its border. I created for that a class that extends from UILabel.

UILabelPadding.swift:

import UIKit

class UILabelPadding: UILabel {

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

   override var intrinsicContentSize : CGSize {
        let superContentSize = super.intrinsicContentSize
        let width = superContentSize.width + padding.left + padding.right
        let heigth = superContentSize.height + padding.top + padding.bottom
        return CGSize(width: width, height: heigth)
    }

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        let superSizeThatFits = super.sizeThatFits(size)
        let width = superSizeThatFits.width + padding.left + padding.right
        let heigth = superSizeThatFits.height + padding.top + padding.bottom
        return CGSize(width: width, height: heigth)
    }


}

and I changed the type of myLabel from UILabel to UILabelPadding. In my UIViewController, I set the text of myLabel, call the sizeToFit() and after that I add the border and the background color to myLabel:

   myLabel.text = "label test"
   myLabel.sizeToFit()
    //background + border
    myLabel.layer.borderColor  = UIColor(red: 27/255, green: 100/255, blue: 90/255,  alpha: 1.0).cgColor
    myLabel.layer.backgroundColor = UIColor(red: 27/255, green: 100/255, blue: 90/255,  alpha: 1.0).cgColor
    myLabel.layer.cornerRadius = 9
    myLabel.layer.masksToBounds = true
    myLabel.layer.borderWidth = 1

The border and the background color are added but the padding is not working. When I debug, the sizeThatFits() is never called.

Any help please?

like image 347
Ne AS Avatar asked Nov 03 '16 15:11

Ne AS


People also ask

How do you add padding to UILabel?

If you have created an UILabel programmatically, replace the UILabel class with the PaddingLabel and add the padding: // Init Label let label = PaddingLabel() label.

How do I add padding to labels in Xcode?

You should use a container view and place the label inside of that. Then add constraints for the label, which will be equivalent to a padding. Select the label and click the constraints panel at the bottom right in the Storyboard file.

What is UILabel?

A view that displays one or more lines of informational text.

How do you label corner radius in Swift?

masksToBounds = true" is an important code, if you apply corner radius to a label and if it dosen't work than adding this will definitely add the corner radius to a particular label.. So this should be the correct answer..


1 Answers

I solved the issue!

For those how are facing the same problem:

1- Make a class extending from UILabel:

UILabelPadding.swift:

class UILabelPadding: UILabel {

    let padding = UIEdgeInsets(top: 2, left: 8, bottom: 2, right: 8)
    override func drawText(in rect: CGRect) {
        super.drawText(in: rect.inset(by: padding))
    }

    override var intrinsicContentSize : CGSize {
        let superContentSize = super.intrinsicContentSize
        let width = superContentSize.width + padding.left + padding.right
        let heigth = superContentSize.height + padding.top + padding.bottom
        return CGSize(width: width, height: heigth)
    }



}

2- Set the type of your label to the UILabelPadding and make sure that the type is set also in the storyboard.

like image 87
Ne AS Avatar answered Sep 20 '22 16:09

Ne AS