公告
财富商城
积分规则
提问
发文
2019-08-07 11:28发布
小情绪 Triste *
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 :)
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)
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) } }
最多设置5个标签!
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:
enoktate's answer updated to Swift 4.2, and written as an extension