UILabel text struck through angle

557 Views Asked by At

I want to create a UILabel in which the text is like this(like angle not horizontally). Can anyone suggest, please?

enter image description here

2

There are 2 best solutions below

0
On BEST ANSWER

All you need to do is just trim the label width to content size.

testLabel.text = "Tk 1,750"
testLabel.frame.size.width = myLabel.intrinsicContentSize.width

and then use as told by @fzh

testLabel.addSlantLine(slantLineColor: UIColor.lightGray,
                   slantLineWidth: 2,
                   startPoint: CGPoint(x: 0, y: testLabel.frame.height - 2),
                   endPoint: CGPoint(x: testLabel.frame.width, y: 2))

Note: Trim after text update

3
On

you can add extension like this:

extension UILabel {
    func addSlantLine(slantLineColor: UIColor, slantLineWidth:CGFloat, startPoint: CGPoint, endPoint: CGPoint) {
        let slantLinePath = UIBezierPath()
        slantLinePath.move(to: startPoint)
        slantLinePath.addLine(to: endPoint)

        let slantLineLayer = CAShapeLayer()
        slantLineLayer.path = slantLinePath.cgPath
        slantLineLayer.lineWidth = slantLineWidth
        slantLineLayer.strokeColor = slantLineColor.cgColor
        layer.addSublayer(slantLineLayer)
    }
}

use:

testLabel.addSlantLine(slantLineColor: UIColor.lightGray,
                       slantLineWidth: 2,
                       startPoint: CGPoint(x: 0, y: testLabel.frame.height - 2),
                       endPoint: CGPoint(x: testLabel.frame.width, y: 2))