2

I have followed this question but didn't solve my problem.

I have a tableview in ViewController , tableviewcell have a label. I want to set attributedString with NSStrikethroughStyleAttributeName. If i am setting complete string as a strikethrough it works but if i am setting as partial it doesn't works.

Below code for success result

let strOriginalPrice = "my price"
let strdiscountedPrice = "discounted price"
let strPrice = strOriginalPrice+" "+strdiscountedPrice

let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: strPrice)
 attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))

cell.lblPrice.attributedText = attributeString 

Below code not working

let attributedDiscunt: NSMutableAttributedString =  NSMutableAttributedString(string: strPrice)
            attributedDiscunt.addAttribute(NSStrikethroughStyleAttributeName, value:2, range: NSMakeRange(0, strOriginalPrice.characters.count-1))

cell.lblPrice.attributedText = attributedDiscunt 
Community
  • 1
  • 1
Vinod Kumar
  • 3,375
  • 1
  • 17
  • 35
  • 1
    iOS 10.3 ? If that's the case: http://stackoverflow.com/questions/43074652/ios-10-3-nsstrikethroughstyleattributename-is-not-rendered-if-applied-to-a-sub/43359207 – Larme May 16 '17 at 09:37
  • Yep, this is a bug in iOS 10.3. Attributes no longer work for a range of your string, only for the whole string. – koen May 16 '17 at 14:48
  • Possible duplicate of [iOS 10.3: NSStrikethroughStyleAttributeName is not rendered if applied to a sub range of NSMutableAttributedString](http://stackoverflow.com/questions/43074652/ios-10-3-nsstrikethroughstyleattributename-is-not-rendered-if-applied-to-a-sub) – Larme May 17 '17 at 06:14
  • @Maddy Since the issue is in fact only reproducible on iOS10.3 due to an iOS Bug, and there is already another question on SO which point it out (in opposition to the answers below which doesn't explain that), it's better in any case to mark the question as duplicate and redirect so to the previous one. – Larme May 17 '17 at 06:16
  • @Maddy And this isn't showed in his code, and what's the real difference then with Lion's or yours answers. Didn't you followed the indication from that other question (adding baseline)? – Larme May 17 '17 at 07:53
  • Both answers have an unfortunate typo, so obviously copy-pasted. – koen May 18 '17 at 17:55

1 Answers1

4

Try this,

   let strOriginalPrice = "my price"
    let strdiscountedPrice = "discounted price"
    let strPrice = strOriginalPrice+" "+strdiscountedPrice

 //        let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: strPrice)
//        attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))

    let attributedDiscunt: NSMutableAttributedString =  NSMutableAttributedString(string: strPrice)
    attributedDiscunt.addAttribute(NSStrikethroughStyleAttributeName, value:2, range: NSMakeRange(0, strOriginalPrice.characters.count-1))
    attributedDiscunt.addAttribute(NSBaselineOffsetAttributeName, value: 0, range: NSMakeRange(0, strOriginalPrice.characters.count-1))


    cell.lblPrice.attributedText = attributedDiscunt
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75