Kishore Kumar
Kishore Kumar

Reputation: 4375

NSStrikethroughStyleAttributeName , How to strike out the string in iOS 10.3?

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 ?

Upvotes: 7

Views: 3203

Answers (7)

Piyush
Piyush

Reputation: 1224

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.

Upvotes: 1

KhanShaheb
KhanShaheb

Reputation: 714

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

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

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.

Upvotes: 8

Vaibhav Gaikwad
Vaibhav Gaikwad

Reputation: 406

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

Upvotes: 0

Anton Gaenko
Anton Gaenko

Reputation: 9015

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

Upvotes: 5

Ray Phan
Ray Phan

Reputation: 41

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

Upvotes: 4

Jonas W
Jonas W

Reputation: 585

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.

Upvotes: 0

Related Questions