删除线不起作用的解决方法

升级iOS10.3之后,给属性文字添加删除线的NSStrikethroughStyleAttributeName不起作用的解决办法。

方法一:

在原有的属性字典里添加NSBaselineOffsetAttributeName(基线偏移值:取值为NSNumber (float),正值上偏,负值下偏)就可以了 。
iOS10.3之前的写法是:

NSAttributedString *attributedString = [[NSAttributedString alloc]
                                        initWithString:goodsprice attributes:@{
                                        NSStrikethroughStyleAttributeName :@(NSUnderlineStyleSingle),
                                        NSStrikethroughColorAttributeName : UIColorFromHexValue(0xBBBBBB)
                                        }];
seckillLabel.attributedText = attributedString;

iOS10.3之后的写法是:

NSAttributedString *attributedString = [[NSAttributedString alloc]
                                       initWithString:goodsprice attributes:@{
                                             NSStrikethroughStyleAttributeName : @(NSUnderlineStyleSingle),
                                             NSStrikethroughColorAttributeName : UIColorFromHexValue(0xBBBBBB),
                                             NSBaselineOffsetAttributeName:@(0)}];//NSBaselineOffsetAttributeName 设置基线偏移值。取值为NSNumber (float),正值上偏,负值下偏
cell.seckillLabel.attributedText = attributedString;
效果图1

补充:

UIFont * font1 = [UIFont systemFontOfSize:40];
UIFont * font2 = [UIFont systemFontOfSize:20];
NSMutableAttributedString * att = [[NSMutableAttributedString alloc] init];
NSAttributedString * att1 = [[NSAttributedString alloc] initWithString:@"20." attributes:@{NSFontAttributeName :font1,NSForegroundColorAttributeName:[UIColor orangeColor]}];
NSAttributedString * att2 = [[NSAttributedString alloc] initWithString:@"00" attributes:@{NSFontAttributeName:font2,NSForegroundColorAttributeName:[UIColor orangeColor]}];
NSAttributedString * att3 = [[NSAttributedString alloc] initWithString:@"%" attributes:@{NSFontAttributeName:font2,NSForegroundColorAttributeName:[UIColor orangeColor],NSBaselineOffsetAttributeName:@15}];
[att appendAttributedString:att1];
[att appendAttributedString:att2];
[att appendAttributedString:att3];
self.myLab.attributedText = att;
效果图2

你可能感兴趣的:(删除线不起作用的解决方法)