代码之家  ›  专栏  ›  技术社区  ›  KhanShaheb

UILabel文本穿过角度

  •  0
  • KhanShaheb  · 技术社区  · 7 年前

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  0
  •   Karthikeyan Bose    7 年前

    您只需将标签宽度修剪为内容大小即可。

    testLabel.text = "Tk 1,750"
    testLabel.frame.size.width = myLabel.intrinsicContentSize.width
    

    testLabel.addSlantLine(slantLineColor: UIColor.lightGray,
                       slantLineWidth: 2,
                       startPoint: CGPoint(x: 0, y: testLabel.frame.height - 2),
                       endPoint: CGPoint(x: testLabel.frame.width, y: 2))
    

    :

        2
  •  3
  •   fzh    7 年前

    您可以像这样添加扩展:

    extension UILabel {
        func addSlantLine(slantLineColor: UIColor, slantLineWidth:CGFloat, startPoint: CGPoint, endPoint: CGPoint) {
            let slantLinePath = UIBezierPath()
            slantLinePath.move(to: startPoint)
            slantLinePath.addLine(to: endPoint)
    
            let slantLineLayer = CAShapeLayer()
            slantLineLayer.path = slantLinePath.cgPath
            slantLineLayer.lineWidth = slantLineWidth
            slantLineLayer.strokeColor = slantLineColor.cgColor
            layer.addSublayer(slantLineLayer)
        }
    }
    

    testLabel.addSlantLine(slantLineColor: UIColor.lightGray,
                           slantLineWidth: 2,
                           startPoint: CGPoint(x: 0, y: testLabel.frame.height - 2),
                           endPoint: CGPoint(x: testLabel.frame.width, y: 2))