UILabel's text with diagonal strikethrough lin

2019-08-07 11:28发布

I would like to make an effect like this, with UILabel text

enter image description here

how can I do that? I didn't find any help on web; I just found help with horizontal line

PS: sorry for my bad english, I'm italian :)

2条回答
老娘就宠你
2楼-- · 2019-08-07 11:53

Looks like it is too late to answer you but this can be helpful for future googlers. A diagonal strikethrough line can be achieved with adding a CAShapeLayer to your label as a sublayer like I did below:

    let linePath = UIBezierPath()
    linePath.moveToPoint(CGPointMake(0, yourLabel.bounds.height))
    linePath.addLineToPoint(CGPointMake(yourLabel.bounds.width, 0))

    let lineLayer = CAShapeLayer()
    lineLayer.path = linePath.CGPath
    lineLayer.lineWidth = 2
    lineLayer.strokeColor = UIColor.lightGrayColor().CGColor
    yourLabel.layer.addSublayer(lineLayer)
查看更多
Fickle 薄情
3楼-- · 2019-08-07 12:06

enoktate's answer updated to Swift 4.2, and written as an extension

extension UILabel {
    /// Strikes through diagonally
    /// - Parameters:
    /// - offsetPercent: Improve visual appearance or flip line completely by passing a value between 0 and 1
    func diagonalStrikeThrough(offsetPercent: CGFloat = 0.1) {
        let linePath = UIBezierPath()
        linePath.move(to: CGPoint(x: 0, y: bounds.height * (1 - offsetPercent)))
        linePath.addLine(to: CGPoint(x: bounds.width, y: bounds.height * offsetPercent))

        let lineLayer = CAShapeLayer()
        lineLayer.path = linePath.cgPath
        lineLayer.lineWidth = 2
        lineLayer.strokeColor = textColor.cgColor
        layer.addSublayer(lineLayer)
    }
}
查看更多
登录 后发表回答