我想通过空格扩展文本

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

我使用NSAttributeString来设置我的文本,我希望它扩展到whitespace

我已经设置了正确的范围,但是通过仅覆盖文本字符。除非我在它之前和之后添加一些非空文本,否则通过忽略空格。

如何在没有额外文本的情况下通过扩展空格来进行罢工?

enter image description here

ios nsattributedstring strikethrough
2个回答
2
投票

我没有找到任何解决方案,但你的最后一个选项意味着添加第一个和最后一个字符作为.与空间你可以尝试一件事。将第一个和最后一个字符的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

在使用NSForegroundColorAttributeNameNSFontAttributeName enter image description here之前

现在你可以使用NSForegroundColorAttributeNameNSFontAttributeName隐藏第一个和最后一个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

使用NSForegroundColorAttributeNameNSFontAttributeName enter image description here


0
投票

对于那些只想要通过空格的水平线的人来说,这就是这样做的方式(Swift 4):

let line = String(repeating: "\u{23AF}", count: 10)
© www.soinside.com 2019 - 2024. All rights reserved.