我还没有找到任何解决方案,但是使用您的最后一个选项意味着将第一个和最后一个字符添加为 . 和空格,您可以尝试一件事。将第一个和最后一个字符的NSForegroundColorAttributeName 设置为标签的背景颜色,或者将NSFontAttributeName 设置为UIFont.systemFont(ofSize: 0.1)。所以它会是这样的。你没有指定你的答案语言,所以我在最新的 Swift 3 中发布答案。
let attributedText = NSMutableAttributedString(string: self.lbl.text!)
attributedText.addAttributes([NSStrikethroughStyleAttributeName: 2], range: NSMakeRange(0, self.lbl.text!.characters.count))
self.lbl.attributedText = attributedText
在使用NSForegroundColorAttributeName & NSFontAttributeName之前
现在您可以使用NSForegroundColorAttributeName 或NSFontAttributeName 隐藏第一个和最后一个dot(.) 字符。
let attributedText = NSMutableAttributedString(string: self.lbl.text!)
attributedText.addAttributes([NSStrikethroughStyleAttributeName: 2], range: NSMakeRange(0, self.lbl.text!.characters.count))
attributedText.addAttributes([NSForegroundColorAttributeName: UIColor.white], range: NSMakeRange(0, 1))
attributedText.addAttributes([NSForegroundColorAttributeName: UIColor.white], range: NSMakeRange(self.lbl.text!.characters.count - 1, 1))
//Or either Set NSFontAttributeName instead of NSForegroundColorAttributeName
//attributedText.addAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 0.1)], range: NSMakeRange(0, 1))
//attributedText.addAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 0.1)], range: NSMakeRange(self.lbl.text!.characters.count - 1, 1))
self.lbl.attributedText = attributedText
使用NSForegroundColorAttributeName或NSFontAttributeName后