在做商品信息展示的时候,通常会需要用到删除线,下面介绍下苹果NSAttributedString自带的删除线属性
通常用法:
NSString *oldPrice = @"¥ 12345";
NSUInteger length = [oldPrice length];
NSMutableAttributedString*attributedString = [[NSMutableAttributedString alloc]initWithString:oldPrice];
[attributedString addAttribute:NSForegroundColorAttributeName value:RGBACOLOR(253, 91, 120, 1) range:NSMakeRange(0, length)];
[attributedString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, length)];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, length)];
[priceLabel setAttributedText:attributedString];
但用了之后发现,删除线在iOS系统10.3以上的手机上消失了。。。查资料发现原来是iOS 10.3中NSStrikethroughStyleAttributeName(任何NSUnderlineStyle情况)都不再起作用,需要换一种写法:
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ 12345"]];
[attrStr addAttributes:@{NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),NSBaselineOffsetAttributeName:@(0)} range:NSMakeRange(0, attrStr.length)];
[self.oldMoneyLab setAttributedText:attrStr];