read

In iOS 7, it's easy to create UILabel with strikethrough text. You can just simply assign NSAttributedString to the text label.

    NSString *price = @"7,500";
    NSString *retailPrice = @"10,000円";
    NSString *text = [NSString stringWithFormat:@"%@ %@", price, retailPrice];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text];
    [string addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)} range:[text rangeOfString:retailPrice]];
    [self.label setAttributedText:string];

nsattributedstring-ios-8-bug

But in iOS 8, that code didn't work anymore. The strikethrough line didn't appear. Funny thing was, if range's location is set to 0, or strikethrough from the beginning, the line appeared. To fix this you just have to assign the strikethrough attribute, NSUnderlineStyleNone to the whole string on initialization.

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleNone)}];
    [string addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle)} range:[text rangeOfString:retailPrice]];
Image

NICNOCQUEE

Blog about iOS, Programming, Japan, or Random Stuff.

« Older Newer »