我想在 swift 4 中画一条删除线,如下图所示。
我使用以下代码绘制了删除线,如下图所示。
func strikeThrough(_ color: UIColor) -> NSAttributedString {
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: self)
let textRange = NSRange(location: 0, length: attributeString.length)
attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: textRange)
attributeString.addAttribute(NSAttributedString.Key.baselineOffset, value: 0, range: textRange)
attributeString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.lightGray, range: textRange)
attributeString.addAttribute(NSAttributedString.Key.strikethroughColor, value: color, range: textRange)
return attributeString
}
关于如何在文本上绘制斜线的任何建议。 如果可能,给我一个使用 Core Graphics 的解决方案。
最佳答案
将 UIBezierPath 与您的 UILabel 一起使用,我们将其命名为标签:
var aPath = UIBezierPath()
aPath.moveToPoint(CGPoint(x: label.frame.origin.x , y:label.frame.origin.y - 10)
aPath.addLineToPoint(CGPoint(x: label.frame.origin.x + label.frame.size.width, y: label.frame.origin.y + label.frame.size.height + 10))
aPath.closePath()
UIColor.redColor().set()
aPath.stroke()
随意玩点数的来源以获得想要的结果。也玩 label.layer.addSublayer(aPath)
关于ios - NSAttributed 文本在 iOS 中带有一条线和一些角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55338027/