如果整个字符串设置中划线,使用以下方法:
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
[attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
[attri addAttribute:NSStrikethroughColorAttributeName value:label.textColor range:NSMakeRange(0, length)];
如果是部分字符串(subString)设置中划线,与上面相似,可以使用一下方法:
NSRange range = [string rangeOfString:subString];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
[attr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle | NSUnderlinePatternSolid) range:range];
[attr addAttribute:NSStrikethroughColorAttributeName value:label.textColor range:range];
但是在iOS8上面的方法设置中划线无效,需要使用以下方法:
NSRange range = [string rangeOfString:subString];
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string attributes:@{NSStrikethroughStyleAttributeName: @(NSUnderlineStyleNone)}];
[attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];