UILabel's text with diagonal strikethrough line

不想你离开。 提交于 2019-12-11 10:05:56

问题


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

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


回答1:


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)



回答2:


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


来源:https://stackoverflow.com/questions/33585420/uilabels-text-with-diagonal-strikethrough-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!