升级ios 10.3后,NSStrikethroughStyleAttributeName属性不管用了,10.3之前的系统都显示正常。
demo如下:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *discountPriceStr = @"95";
NSString *unitStr = @"元";
NSString *originalPriceStr = @"220";
NSString *spaceStr = @" ";
NSString *textStr = [NSString stringWithFormat:@"%@%@%@%@%@", discountPriceStr, unitStr, spaceStr, originalPriceStr, unitStr];
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:textStr];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, discountPriceStr.length)];
[attributeString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(discountPriceStr.length, textStr.length - discountPriceStr.length)];
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10.0f] range:NSMakeRange(0, discountPriceStr.length)];
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:8.0f] range:NSMakeRange(discountPriceStr.length, textStr.length - discountPriceStr.length)];
[attributeString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleNone) range:NSMakeRange(0, discountPriceStr.length + unitStr.length + spaceStr.length)];
[attributeString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(discountPriceStr.length + unitStr.length + spaceStr.length, originalPriceStr.length + unitStr.length)];
[attributeString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blackColor] range:NSMakeRange(discountPriceStr.length + unitStr.length + spaceStr.length, originalPriceStr.length + unitStr.length)];
_priceLabel.attributedText = attributeString;
}
@end