如何在 swift 4 中交叉点击单元格按钮

How to Cross strike cell button in swift 4

如何在 swift 4

中交叉点击单元格按钮

目前我正在使用下面的代码点击按钮

func testString (titleStr : String) -> NSMutableAttributedString {
    let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: titleStr)
    attributeString.addAttribute(NSBaselineOffsetAttributeName, value: 1, range: NSMakeRange(0, attributeString.length ))
    attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 1, range: NSMakeRange(0, attributeString.length  ))
    attributeString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.red, range:  NSMakeRange(0, attributeString.length))
    return attributeString
}

然后像这样得到输出1:

如何更改我的代码以获得这样的输出 屏幕截图 1:

嗯,NSAttributedString 不支持 "cross-strike"。 您必须在 UILabel 上添加带十字图标的 ImageView。

您可以使用 UIBezierPath 来实现。在按钮上方创建 UIView 并设置按钮边缘的约束 - 鼠标右键单击视图并拖动到按钮:

保持shift和select所有边:

然后使用覆盖 draw(_ rect: CGRect) 方法创建自定义 UIView class:

final class CrossedView: UIView {

    override func draw(_ rect: CGRect) {
        let path = UIBezierPath()

        path.move(to: .zero)
        path.addLine(to: CGPoint(x: frame.width, y: frame.height))

        path .move(to: CGPoint(x: 0, y: frame.height))
        path.addLine(to: CGPoint(x: frame.width, y: 0))

        UIColor.gray.setStroke()
        path.stroke()
    }

}

接下来,在故事板中将 Class 设置为 CrossedView,运行。

最后,你得到了预期的结果: