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

问题描述 投票:0回答:2

如何在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
}

得到输出像这样1enter image description here

如何更改我的代码以获得这样的输出截图qazxsw poi:qazxsw poi

swift strikethrough
2个回答
1
投票

你可以通过使用1实现这一目标。在按钮上方创建enter image description here并为按钮边缘设置约束 - 在视图上单击鼠标右键并拖动到按钮:

UIBezierPath

按住UIView并选择所有边缘:

enter image description here

然后使用覆盖shift方法创建自定义enter image description here类:

UIView

接下来,在故事板中将draw(_ rect: CGRect)设置为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


0
投票

那么NSAttributedString不支持“交叉打击”。您必须在UILabel上添加带有交叉打击图标的ImageView。

© www.soinside.com 2019 - 2024. All rights reserved.