简体   繁体   中英

NSMutableAttributedString's attribute NSStrikethroughStyleAttributeName doesn't work correctly in iOS8

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:

aaaa bbbb

But when I exchange their positions:

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

It display correctly in iOS7 but not correctly in iOS8

ios7: bbbb aaaa 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?

NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"bbbb" attributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]}];

它应该有所帮助

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

修复了iOS8中的错误:Strike通过字符串的第一个字母和清晰的颜色,然后在你想要的范围内打击文本。

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

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

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM