【问题标题】:UILabel's text with diagonal strikethrough line带有对角删除线的 UILabel 文本
【发布时间】:2015-11-07 17:07:50
【问题描述】:

我想做一个这样的效果,用 UILabel 文字

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

PS:对不起我的英语不好,我是意大利人:)

【问题讨论】:

    标签: label uilabel strikethrough


    【解决方案1】:

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

    【讨论】:

    • 如果这对任何人都不起作用,请务必在致电 myLabel. diagonalStrikeThrough() 之前致电 myLabel.sizeToFit()
    【解决方案2】:

    现在回答您似乎为时已晚,但这对未来的 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多