NSMutableAttributedString 的删除线厚度

Strikethrough thickness for NSMutableAttributedString

我在 objective-c 中用删除线注释可变属性字符串。使用以下代码。

[MAString addAttribute:NSStrikethroughStyleAttributeName
                     value:[NSNumber numberWithInt:1]
                     range:NSMakeRange(0, [text length])]

我不清楚 value 参数控制什么以及它可能的值是什么。它控制厚度吗?如果是,可能的值是多少?

看看 documentation 怎么样?

This value indicates whether the text has a line through it and corresponds to one of the constants described in NSUnderlineStyle. The default value for this attribute is styleNone.

然后:

https://developer.apple.com/reference/uikit/nsunderlinestyle

此答案适合懒惰的程序员

extension String {

/// Apply strike font on text
    func strikeThrough(lineThickness:Int,lineColor:UIColor) -> NSAttributedString {
  let attributeString = NSMutableAttributedString(string: self)
    attributeString.addAttribute(
    NSAttributedString.Key.strikethroughStyle,
    value: lineThickness,
    range: NSRange(location: 0, length: attributeString.length))
    attributeString.addAttribute(
    NSAttributedString.Key.strikethroughColor,
    value: lineColor,
    range: NSRange(location: 0, length: attributeString.length))
    return attributeString
   }
 }

用法

yourlabel.attributedText = "your text".strikeThrough(lineThickness: 3, lineColor: .red)