NSMutableAttributedString's attribute NSStrikethroughStyleAttributeName doesn't work correctly in iOS8
Asked Answered
J

6

7

I have a mutable attributed string without NSStrikethroughStyleAttributeName attribute like this:

NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@"aaaa" attributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]}];

and another mutable attributed string with NSStrikethroughStyleAttributeName attribute like this:

NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"bbbb"];

and a whole string containing two strings above:

NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] init];
[labelString appendAttributedString:str1];
[labelString appendAttributedString:str2];

when I attach the whole string to a UILabel:

_label.attributedText = labelString;

It both display well in iOS7 and iOS8, like this:

aaaabbbb

But when I exchange their positions:

[labelString appendAttributedString:str2];
[labelString appendAttributedString:str1];

It display correctly in iOS7 but not correctly in iOS8

ios7: bbbbaaaa ios8: bbbbaaaa

It seems that in iOS8, UILabel doesn't render the strikethrough correctly if the first character in the attributed string is not strikethroughed. I think this is a bug in iOS8, is there anyone who encounter the same problem with me?

Janitajanith answered 21/9, 2014 at 5:40 Comment(3)
I am having exactly the same problem. Have you raised a radar?Beefcake
FYI I have just added radar #18409995.Beefcake
I've also added an open radar now: openradar.appspot.com/18409995 (been raising so many IOS8 thought I may as well learn how open radar works)Beefcake
M
10
NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"bbbb" attributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]}];

it should help

Medick answered 22/9, 2014 at 15:4 Comment(1)
This also magically fixed my problem. I was attempting to highlight part of an attributed string by setting the NSBackgroundColorAttributeName with a color and an NSRange. The code worked perfectly on iOS 7, but I found that in iOS 8 the highlighting only worked if the range had a location of 0; any other value and it wasn't drawn. Creating the attributed string with your code fixed my problem somehow.Fogbow
I
8

On iOS8.1.1 we have more problems with setting NSMutableAttributedString, like with NSStrikethroughStyleAttributeName.

If your attributes are not changing try this and it will be.

I started reasoning by the UILabel that is spirit to chaneger attributes, so if we assume that the NSStrikethroughStyleAttributeName not initialized the label text so we will need the initialize it .

What I do is initializing mutableAttributedString by a none NSStrikethroughStyleAttributeName and change this attribute to highlight: result is:

 

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString: myString attributes: @ {NSStrikethroughStyleAttributeName: @ (NSUnderlineStyleNone)}];
             [string addAttributes: @ {NSStrikethroughStyleAttributeName: @ (NSUnderlineStyleSingle)} range: [myString rangeOfString: mySubString]];
[MyLabel setAttributedText: string];

effective result <= iOS8.1.1

Inland answered 24/11, 2014 at 13:38 Comment(1)
It works when MyLabel.numberOfLines is NOT 1. I tested it in iOS 8.1 simulator.Rowland
R
3

Fix for the bug in iOS8: Strike Through first letter of the string with clear color and then Strike through the text in which ever range you want.

Raisaraise answered 25/9, 2014 at 11:49 Comment(0)
E
1

When you are not starting the index from 0, try this:

NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:@"Hello"];
[attributedStr addAttributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle)} range:NSMakeRange(1, str.length)];
[attributedStr addAttributes:@{NSStrikethroughStyleAttributeName : @(NSUnderlineStyleNone)} range:NSMakeRange(0, 1)];
Eisenstark answered 28/5, 2015 at 2:29 Comment(0)
Z
1

When you add NSStrikethroughStyleAttributeName, you should also add NSBaselineOffsetAttributeName, which is set to 0. And in swift, you should use the rawValue of NSUnderlineStyle.

enter image description here

Zoraidazorana answered 22/6, 2017 at 9:3 Comment(0)
U
0

I see the same issue and changing NSStrikethroughStyleAttributeName's value to NSUnderlineStyleNone doesn't fix it. Sure seems like iOS 8 bug.

Unwarranted answered 24/9, 2014 at 0:33 Comment(1)
Don't change the value to NSUnderlineStyleNone. Just add a NSStrikethroughStyleAttributeName attribute with value NSUnderlineStyleNone to those text which is not strikethroughed.Janitajanith

© 2022 - 2024 — McMap. All rights reserved.