UILabel 文字划过角度
UILabel text struck through angle
我想创建一个 UILabel,其中的文本是这样的(比如角度不是水平的)。请问有人可以推荐吗?
您可以像这样添加扩展名:
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))
您只需 trim 将标签宽度调整为内容大小即可。
testLabel.text = "Tk 1,750"
testLabel.frame.size.width = myLabel.intrinsicContentSize.width
然后按照@fzh
的说明使用
testLabel.addSlantLine(slantLineColor: UIColor.lightGray,
slantLineWidth: 2,
startPoint: CGPoint(x: 0, y: testLabel.frame.height - 2),
endPoint: CGPoint(x: testLabel.frame.width, y: 2))
注意:
Trim 文本更新后
我想创建一个 UILabel,其中的文本是这样的(比如角度不是水平的)。请问有人可以推荐吗?
您可以像这样添加扩展名:
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))
您只需 trim 将标签宽度调整为内容大小即可。
testLabel.text = "Tk 1,750"
testLabel.frame.size.width = myLabel.intrinsicContentSize.width
然后按照@fzh
的说明使用testLabel.addSlantLine(slantLineColor: UIColor.lightGray,
slantLineWidth: 2,
startPoint: CGPoint(x: 0, y: testLabel.frame.height - 2),
endPoint: CGPoint(x: testLabel.frame.width, y: 2))
注意: Trim 文本更新后