使用Swift在iOS 11中为NSAttributedString添加删除线

时间:2018-03-21 23:07:49

标签: ios swift nsattributedstring strikethrough

有一些问题得到了删除工作。目前我正在做以下事情:

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

但它并没有显示任何形式的删除线。有任何想法吗?我似乎找不到任何可行的东西。

6 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:2)

迅速4.1

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

答案 2 :(得分:1)

尝试一下

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
}

label.attributedText = "Hello world".strikethrough("world")

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

迅速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 = mutFollowersString

答案:

enter image description here

答案 5 :(得分:0)

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))