iOS富文本开发之删除线

1.富文本添加删除线, 先看代码

  NSString *priceStr = [NSString stringWithFormat:@"体检费用:%.f  元", detailModel.price];
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:priceStr];
 [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:@"#FF7352"] range:NSMakeRange(5, priceStr.length - 5)];
_priceLabel.attributedText = attStr;
NSString *marketPriceStr = [NSString stringWithFormat:@"市场价:%.f元", detailModel.market_price];
 NSMutableAttributedString *attPrice = [[NSMutableAttributedString alloc]initWithString:marketPriceStr];
 [attPrice addAttribute:NSBaselineOffsetAttributeName value:@(0) range:NSMakeRange(4,marketPriceStr.length - 4)];
 [attPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | 
NSUnderlineStyleSingle)range:NSMakeRange(4, marketPriceStr.length - 4)]; _marketPriceLabel.attributedText = attPrice;

NSBaselineOffsetAttributeName 设置基线偏移值。取值为NSNumber (float),正值上偏,负值下偏;

重点: 如果不设置富文本的基线偏移值, 就无法成功添加删除线

你可能感兴趣的:(iOS富文本开发之删除线)