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

后端 未结 6 1183
猫巷女王i
猫巷女王i 2020-12-31 21:36

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

NSMutableAttributedString *str1 = [[NSMutableAttributedStri         


        
相关标签:
6条回答
  • 2020-12-31 21:55

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

    0 讨论(0)
  • 2020-12-31 21:58

    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)];
    
    0 讨论(0)
  • 2020-12-31 22:04

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

    0 讨论(0)
  • 2020-12-31 22:13

    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

    0 讨论(0)
  • 2020-12-31 22:14
    NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"bbbb" attributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleNone]}];
    

    it should help

    0 讨论(0)
  • 2020-12-31 22:17

    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.

    0 讨论(0)
提交回复
热议问题