First, generate a class that inherits from UILabel SFPStrikeThroughAndUnderLineLabel
1. In the. h file
@ Interface SFPStrikeThroughAndUnderLineLabel: UILabel
{
BOOL _ isWithStrikeThrough;
}
@ Property (nonatomic, assign) BOOL isWithStrikeThrough; // controls whether to display strikethrough.
@ Property (nonatomic, assign) CGFloat strikeThroughLineWidth; // you can specify the strikethrough width.
@ Property (nonatomic, retain) NSArray * strikeThroughRGBAlphaArray; // sets the strikethrough color. The elements in the array are RGB and alpha values, which means that the array has four elements.
# Import "SFPStrikeThroughAndUnderLineLabel. h"
@ Implementation SFPStrikeThroughAndUnderLineLabel
@ Synthesize isWithStrikeThrough = _ isWithStrikeThrough;
@ Synthesize strikeThroughRGBAlphaArray = _ strikeThroughRGBAlphaArray;
@ Synthesize strikeThroughLineWidth = _ strikeThroughLineWidth;
-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self ){
// Initialization code
Self. strikeThroughRGBAlphaArray = [[NSArrayalloc] initWithObjects: @ "0", @ "0", @ "0", @ "1", nil]; // by default, the color of the strikethrough is black and non-transparent. a default value is provided. This attribute does not need to be assigned a value when an object is created.
Self. strikeThroughLineWidth = 1; // The default strikethrough width is 1pix
}
Returnself;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
-(Void) drawRect :( CGRect) rect
{
// Drawing code
If (self. isWithStrikeThrough) // display strikethrough
{
CGContextRef c = UIGraphicsGetCurrentContext ();
CGFloat color [4] = {[self. strikethroughrgbalph?rayobjectatindex: 0] floatValue], [[self. strikethroughrgbalph?rayobjectatindex: 1] floatValue], [[self. strikethroughrgbalph?rayobjectatindex: 2] floatValue], [[self. strikethroughrgbalph?rayobjectatindex: 3] floatValue]};
CGContextSetStrokeColor (c, color );
CGContextSetLineWidth (c, self. strikeThroughLineWidth );
CGContextBeginPath (c );
CGFloat halfWayUp = (self. bounds. size. height-self. bounds. origin. y)/2.0;
CGContextMoveToPoint (c, self. bounds. origin. x, halfWayUp );
CGContextAddLineToPoint (c, self. bounds. origin. x + self. bounds. size. width, halfWayUp );
CGContextStrokePath (c );
}
[Super drawRect: rect];
}
3. Use: Add SFPStrikeThroughAndUnderLineLabel. h file
SFPStrikeThroughAndUnderLineLabel * oneLabel = [[SFPStrikeThroughAndUnderLineLabelalloc] initWithFrame: CGRectMake (20, 20,100,200)];
OneLabel. backgroundColor = [UIColorwhiteColor];
OneLabel. numberOfLines = 0;
OneLabel. text = @ "Zhu Dongling, I love you for 10 thousand years; Zhu Dongling, I love you for 10 thousand years; Zhu Dongling, I love you for 10 thousand years; Zhu Dongling, I love you for 10 thousand years; Zhu Dongling, I love you for 10 thousand years ";
OneLabel. isWithStrikeThrough = YES;
// The following two attributes can be set with no value, and all of them have default values.
// OneLabel. rgbAlphaArray = [NSArray arrayWithObjects: @ "255", @ "255", @ "0", @ "1", nil];
// OneLabel. strikeThroughLineWidth = 20;
[Self. view addSubview: oneLabel];
Note: This is to delete the entire UILabel control (a label only has one strikethrough), rather than adding strikethrough to the label text.