7

I have used this line of code before release of iOS 10.3 ,and worked fine.

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",strMRP,strOffer]];

[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, strMRP.length)];

[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(strMRP.length, strOffer.length+1)];

[attributeString addAttribute:NSStrikethroughStyleAttributeName
                        value:[NSNumber numberWithInteger: NSUnderlineStyleDouble]
                        range:NSMakeRange(0,strMRP.length)];

But now it is stopped working ,is there any alternate way to do the strike out ?

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47
  • So, what is the error exactly?... I have successfully reproduced your snippet in a swift playground (iOS 10.3) and works just fine. – Alladinian Mar 28 '17 at 13:10
  • @Alladinian no its not working ,can you test it on device – Kishore Kumar Mar 28 '17 at 13:31
  • Yep, it fails on a device, so it's definitely a bug – Alladinian Mar 28 '17 at 13:35
  • It also happens with subscript and superscript attributes – koen Apr 05 '17 at 15:21
  • See for a possible solution here: http://stackoverflow.com/questions/42478697/swift-3-1-nssuperscript-in-nsattributedstring-not-working-as-expected/43074313 – koen Apr 05 '17 at 16:29
  • 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) – tommybananas Oct 20 '17 at 15:08

7 Answers7

8

it is the bug in iOS 10.3 , NSStrikethroughStyleAttributeName (any NSUnderlineStyle cases) is not working any more on iOS SDK 10.3.

if anyone found the updated answer related to this , please inform here, I will update my answer.

Product Version: 10.3

Created: 14-Mar-2017

Originated: 14-Mar-2017

Open Radar Link: http://www.openradar.appspot.com/31034683

Radar status is Currently Open state

you can see the alternate sample also here may be it useful.

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
5

I found one workaround on developer forum, which works for me. Adding of NSBaselineOffsetAttributeName to string attributes fixed this problem :)

Anton Gaenko
  • 8,929
  • 6
  • 44
  • 39
4

It work fine with

   NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",strMRP,strOffer]];

[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, strMRP.length)];

[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(strMRP.length, strOffer.length+1)];

[attributeString addAttribute:NSBaselineOffsetAttributeName
                        value:[NSNumber numberWithInteger: NSUnderlineStyleNone]
                        range:NSMakeRange(0,strMRP.length)];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
                        value:[NSNumber numberWithInteger: NSUnderlineStyleDouble]
                        range:NSMakeRange(0,strMRP.length)];
Ray Phan
  • 41
  • 2
1

iOS 10.3 onward you need to add NSBaselineOffsetAttributeName.

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",strMRP,strOffer]];
[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12] range:NSMakeRange(0, strMRP.length)];
[attributeString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(strMRP.length, strOffer.length+1)];
[attributeString addAttribute:NSBaselineOffsetAttributeName
                    value:[NSNumber numberWithInteger: NSUnderlineStyleNone]
                    range:NSMakeRange(0,strMRP.length)];
[attributeString addAttribute:NSStrikethroughStyleAttributeName
                    value:[NSNumber numberWithInteger: NSUnderlineStyleDouble]
                    range:NSMakeRange(0,strMRP.length)];

Once you add NSBaselineOffsetAttributeName then it works for single line, double line ect.

Piyush
  • 1,156
  • 12
  • 20
0

As mentioned above, this is an iOS 10.3 bug.

We needed an immediate workaround, so just in case anyone is looking for hints: Our Label had attributes set through both NSMutableAttributedString as well as NSMutableParagraphStyle. The bug did not occur when using no / an "empty" paragraph style (instance without any properties set).

So in this scenario, omitting the paragraph style and working around the then missing paragraph properties resolved the issue for us.

Jonas W
  • 585
  • 4
  • 11
0

Just Use this :-

NSMutableAttributedString *costPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"₹ %@",strDetails]]; [costPrice addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithInteger: NSUnderlineStyleSingle] range:NSMakeRange(0,costPrice.length)];

This is temp solution . Hope it works

Vaibhav Gaikwad
  • 396
  • 5
  • 13
0

***You can pass it to function & Enjoy !!!

func customString(currentprice:String,oldPrice:String) -> NSMutableAttributedString{
        // 1
        let NewString = currentprice + "  " + oldPrice

        let string = NewString as NSString
        let attributedString = NSMutableAttributedString(string: string as String)

        // 2
        let firstAttributes = [NSForegroundColorAttributeName: UIColor(red: 238/255, green: 140/255, blue: 84/255, alpha: 1),NSBaselineOffsetAttributeName:1]
        let secondAttributes = [NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSStrikethroughStyleAttributeName: 1]

        // 3
        attributedString.addAttributes(firstAttributes, range: string.rangeOfString(currentprice))
        attributedString.addAttributes(secondAttributes, range: string.rangeOfString(oldPrice))

        return attributedString
    }

and use like:

YourUILabel.attributedText   = customString("300", oldPrice: "400")
KhanShaheb
  • 714
  • 2
  • 11
  • 26