2

I am trying to create a strikethrough effect on part of a UILabel text, so fat without success. I have noticed that I can create this effect on ALL the text, but not just part of it. Here's what I have

let text = "\(20.4$) \(On Rush Hour)"
let attrStr = NSMutableAttributedString(string: text)
let range = (text as NSString).range(of: "20.4$")
attrStr.addAttribute(NSStrikethroughStyleAttributeName,
                     value: NSUnderlineStyle.styleSingle.rawValue,
                     range: range)
self.originalPriceLabel.attributedText = attrStr

Now, if I only replace

let range = (text as NSString).range(of: "20.4$")

with

let range = (text as NSString).range(of: text)

I see the strikethrough. What am I doing wrong?

Thanks!

shallowThought
  • 19,212
  • 9
  • 65
  • 112
Yotam
  • 9,789
  • 13
  • 47
  • 68
  • 4
    There is a bug in iOS10.3: https://stackoverflow.com/questions/43074652/ios-10-3-nsstrikethroughstyleattributename-is-not-rendered-if-applied-to-a-sub/43359207 ? – Larme May 24 '17 at 08:16
  • Another way is to make two attributed strings, one with strike through and other without it and then concatenate them .. and set them to your label .. – Hobbit May 24 '17 at 08:21
  • that worked @Larme, thanks!! – Yotam May 24 '17 at 08:33
  • Possible duplicate of [iOS 10.3: NSStrikethroughStyleAttributeName is not rendered if applied to a sub range of NSMutableAttributedString](https://stackoverflow.com/questions/43074652/ios-10-3-nsstrikethroughstyleattributename-is-not-rendered-if-applied-to-a-sub) – Larme May 24 '17 at 11:56
  • iOS 11 seems to handle this fine. – Tom Hamming Jun 29 '17 at 22:15

0 Answers0