使用Swift将删除线添加到iOS 11中的NSAttributedString

esk*_*imo 2 nsattributedstring strikethrough ios swift

遇到一些删除线才能正常工作的问题。目前,我正在执行以下操作:

theString.addAttributes([
        NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue, 
        NSAttributedStringKey.strikethroughColor: UIColor.white
    ], range: NSMakeRange(0, 1))
Run Code Online (Sandbox Code Playgroud)

它没有显示任何形式的删除线。有任何想法吗?我似乎找不到任何有效的方法。

Fit*_*syu 6

我用它来删除Xcode9 / iOS11 / Swift4 env中的文本

  let strokeEffect: [NSAttributedStringKey : Any] = [
        NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue,
        NSAttributedStringKey.strikethroughColor: UIColor.red,
    ]

   let strokeString = NSAttributedString(string: "text", attributes: strokeEffect)
Run Code Online (Sandbox Code Playgroud)


Amr*_*ari 6

斯威夫特 5.1

let attributedText : NSMutableAttributedString =  NSMutableAttributedString(string: "Your Text")
attributedText.addAttributes([
                NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
                NSAttributedString.Key.strikethroughColor: UIColor.lightGray,
                NSAttributedString.Key.font : UIFont.systemFont(ofSize: 12.0)
                ], range: NSMakeRange(0, attributedText.length))
Run Code Online (Sandbox Code Playgroud)


Alf*_*Bae 0

我正在分享最新动态。对于 Swift4、iOS 11.3

    attributedText.addAttributes([ 
    NSStrikethroughStyleAttributeName: 
    NSUnderlineStyle.styleSingle.rawValue, 
    NSStrikethroughColorAttributeName: 
    UIColor.black], range: textRange)
Run Code Online (Sandbox Code Playgroud)