Add bottom bar in Label iOS swift

 Problem:

I want to add the line in the bottom of the label.If i set the border then label can had line around the label.But i need only in the bottom of the label.

How can i achieve this?

Solution:

You can add the extension like below to achieve that.

extension UILabel {

    func addBootomBorder() {

        if self.text != "" {

        let bottomLayer = CALayer()

        bottomLayer.frame = CGRect(x: 0, yframe.height - 1, widthself.bounds.size.widthheight: 1)

        bottomLayer.backgroundColor = UIColor.lightGray.cgColor

        self.layer.addSublayer(bottomLayer)

        }


    }


    override open func layoutSubviews() {

        if let lay = self.layer.sublayers?[0] {

            lay.frame.size.width = self.bounds.width

        }

    }

}


You can use the above extension to achieve that.Also layoutSubviews() method is used for if you had change the constraint of the label in run time then also the bottom layer wants to be extended.If you not added the function it will never be updated the sublayer width while constraint changes in that label.


Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift