13

Having some issues getting strikethrough to work. Currently I'm doing the following:

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

It's not showing any sort of strikethrough though. Any ideas? I can't seem to find anything that works.

Willeke
  • 14,578
  • 4
  • 19
  • 47
eskimo
  • 304
  • 1
  • 3
  • 15
  • Your code is working for me. Though only the first letter is getting the strikethrough given the range you have. Try different colors. Maybe you can't see the white strikethrough if the background is white. – rmaddy Mar 21 '18 at 23:19
  • Ummmm hmmmmmm why can't I see it in my label -_- – eskimo Mar 21 '18 at 23:38
  • Show more relevant code. I tested in a playground without a label. – rmaddy Mar 21 '18 at 23:45
  • 1
    There isn't really anything else relevant, using almost identical code to underline text works perfectly. Does strikethrough maybe only work in a textview and not a label? – eskimo Mar 21 '18 at 23:46
  • Just used a label in a playground. Still working for me. Maybe a font issue or a colors issue. That's why you need to provide more details. – rmaddy Mar 21 '18 at 23:48
  • Yeah it's working in a playground for me too. Lemme see if maybe it's the font I'm using? – eskimo Mar 22 '18 at 00:00
  • Hrmmm, seems to be something with TTTAttributedLabel. I got it to work with kTTTStrikeOutAttributeName instead, but I can't set the color that way it seems :/ – eskimo Mar 22 '18 at 00:22

6 Answers6

12

I use this to strike through a text in Xcode9/iOS11/Swift4 env

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

   let strokeString = NSAttributedString(string: "text", attributes: strokeEffect)
Fitsyu
  • 860
  • 11
  • 19
6

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))
Amrit Tiwari
  • 922
  • 7
  • 21
4

swift 4.1

attributedText.addAttributes([
NSAttributedStringKey.strikethroughStyle:NSUnderlineStyle.styleSingle.rawValue, NSAttributedStringKey.strikethroughColor:UIColor.black],
range: NSMakeRange(0, attributedText.length))
Osman
  • 1,496
  • 18
  • 22
1

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

ANSWER :

enter image description here

McDonal_11
  • 3,935
  • 6
  • 24
  • 55
Krunal Patel
  • 1,649
  • 1
  • 14
  • 22
1

Try this

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")
Yuyutsu
  • 2,509
  • 22
  • 38
0

I am sharing latest updates. For Swift4, iOS 11.3

    attributedText.addAttributes([ 
    NSStrikethroughStyleAttributeName: 
    NSUnderlineStyle.styleSingle.rawValue, 
    NSStrikethroughColorAttributeName: 
    UIColor.black], range: textRange)
Alf Bae
  • 546
  • 6
  • 10