1.方法一
NSString *oldPrice = model.marketprice;
NSUInteger length = [oldPrice length];
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:oldPrice];
[attri addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, length)];
[attri addAttribute:NSStrikethroughColorAttributeName value:[RuiguColors GetRuiguFourNumberColor] range:NSMakeRange(0, length)];
[_originalPriceLabel setAttributedText:attri];
2.方法二
@property (assign, nonatomic) BOOL strikeThroughEnabled; // 是否画线
@property (strong, nonatomic) UIColor *strikeThroughColor; // 画线颜色
- (void)drawTextInRect:(CGRect)rect
{
[super drawTextInRect:rect];
CGSize textSize = [[self text] sizeWithFont:[self font]];
NSLog(@"______textSize = %@ , ______rect = %@",NSStringFromCGSize(textSize),NSStringFromCGRect(rect));
CGFloat strikeWidth = textSize.width;
CGRect lineRect;
if ([self textAlignment] == NSTextAlignmentRight)
{
// 画线居中
lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2, strikeWidth, 1);
// 画线居下
//lineRect = CGRectMake(rect.size.width - strikeWidth, rect.size.height/2 + textSize.height/2, strikeWidth, 1);
}
else if ([self textAlignment] == NSTextAlignmentCenter)
{
// 画线居中
lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2, strikeWidth, 1);
// 画线居下
//lineRect = CGRectMake(rect.size.width/2 - strikeWidth/2, rect.size.height/2 + textSize.height/2, strikeWidth, 1);
}
else
{
// 画线居中
lineRect = CGRectMake(0, rect.size.height/2, strikeWidth, 1);
// 画线居下
//lineRect = CGRectMake(0, rect.size.height/2 + textSize.height/2, strikeWidth, 1);
}
if (self.strikeThroughEnabled)
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [self strikeThroughColor].CGColor);
CGContextFillRect(context, lineRect);
}
}