使用Swift将删除线添加到iOS 11中的NSAttribute字符串

使用Swift将删除线添加到iOS 11中的NSAttribute字符串,ios,swift,nsattributedstring,strikethrough,Ios,Swift,Nsattributedstring,Strikethrough,在工作中遇到一些问题。目前我正在做以下工作: theString.addAttributes([ NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue, NSAttributedStringKey.strikethroughColor: UIColor.white ], range: NSMakeRange(0, 1)) 但它没有显示任何罢工的迹象

在工作中遇到一些问题。目前我正在做以下工作:

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

但它没有显示任何罢工的迹象。有什么想法吗?我似乎找不到任何有效的工具。

我正在共享最新更新。对于Swift4,iOS 11.3

    attributedText.addAttributes([ 
    NSStrikethroughStyleAttributeName: 
    NSUnderlineStyle.styleSingle.rawValue, 
    NSStrikethroughColorAttributeName: 
    UIColor.black], range: textRange)

我使用它在Xcode9/iOS11/Swift4 env中遍历一个文本

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

   let strokeString = NSAttributedString(string: "text", attributes: strokeEffect)
swift 4.1

attributedText.addAttributes([
NSAttributedStringKey.strikethroughStyle:NSUnderlineStyle.styleSingle.rawValue, NSAttributedStringKey.strikethroughColor:UIColor.black],
range: NSMakeRange(0, attributedText.length))
swift 4.2

    let mutPricesString : NSMutableAttributedString = NSMutableAttributedString()
    var attrPrice_1 : NSAttributedString = NSAttributedString()
    var attrPrice_2 : NSAttributedString = NSAttributedString()

    attrPrice_1 = NSAttributedString(string: "14000 KD", attributes:
            [NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue,
             NSAttributedString.Key.foregroundColor: UIColor.lightGray,
             NSAttributedString.Key.font: ARFont.Regular(fontSize: 15)])

    attrPrice_2 = NSAttributedString(string: " 12460 KD", attributes:
        [NSAttributedString.Key.foregroundColor: UIColor.black,
         NSAttributedString.Key.font: ARFont.Semibold(fontSize: 17)])

    mutPricesString.append(attrPrice_1)
    mutPricesString.append(attrPrice_2)
    
    lbl.attributedText = mutPricesString
答复:


Swift 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))
试试这个

func strikethrough(_ text:String) -> NSAttributedString{
        //strikethrough
        let range = (self as NSString).range(of: text)
        let attributedString = NSMutableAttributedString(string:self)
        attributedString.addAttribute(NSAttributedString.Key.strikethroughColor, value: UIColor.gray, range: range)
        attributedString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: range)
        
        return attributedString
}


你的代码对我有用。虽然只有第一个字母会在给定的范围内得到删除线。试试不同的颜色。如果背景是白色的,你可能看不到白色的删除线。嗯,嗯,嗯,嗯,为什么我不能在我的标签中看到它?显示更多相关的代码。我在一个没有标签的操场上进行了测试。实际上没有任何其他相关的东西,使用几乎相同的代码在文本下面划线非常有效。删除线是否仅在文本视图中有效,而不是标签?只是在操场中使用标签。还在为我工作。可能是字体问题或颜色问题。这就是为什么你需要提供更多的细节。不要使用像“2”这样的硬编码常量。像其他答案一样使用实际库常量。
label.attributedText = "Hello world".strikethrough("world")