UILabel的文字带有对角线删除线

时间:2015-11-07 17:07:51

标签: label uilabel strikethrough

我想用UILabel文本

制作这样的效果

enter image description here

我该怎么做?我没有在网上找到任何帮助;我刚刚找到了水平线的帮助

PS:抱歉我的英语不好,我是意大利人:)

2 个答案:

答案 0 :(得分:3)

回答你似乎为时已晚,但这对未来的google可能会有所帮助。 如下所示,将CAShapeLayer添加到您的标签作为子图层可以实现对角线删除线:

    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)

答案 1 :(得分:2)

enoktate的答案已更新为 Swift 4.2 ,并作为扩展名写成

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)
    }
}