1. Create a new class name for Strikelabel, which is a subclass of Uilabel;
2. @property (nonatomic) BOOL strikethroughenabled in StrikeLabel.h;
In the STRIKELABEL.M.
-(void) DrawRect: (CGRect) rect{
[Super Drawtextinrect:rect];
Cgsize textSize = [[self text] sizewithfont:[self font];
CGFloat strikewidth = textsize.width;
CGRect Linerect;
if ([self textalignment] = = uitextalignmentright) {
Linerect = CGRectMake (Rect.size.width-strikewidth, RECT.SIZE.HEIGHT/2, Strikewidth, 1);
} else if ([self textalignment] = = Uitextalignmentcenter) {
Linerect = CGRectMake (RECT.SIZE.WIDTH/2-STRIKEWIDTH/2, RECT.SIZE.HEIGHT/2, Strikewidth, 1);
} else {
Linerect = CGRectMake (0, RECT.SIZE.HEIGHT/2, strikewidth, 1);
}
if (_strikethroughenabled) {
Cgcontextref context = Uigraphicsgetcurrentcontext ();
Cgcontextfillrect (context, linerect);
}}
3. When you need to declare a label
Strikelabel *label=[[strikelabel alloc]initwithframe:cgrectmake (100, 100, 200, 100)];
[Email protected] "Wo ai ni";
Label.strikethroughenabled=yes;
[Self.view Addsubview:label];
It's done.
How iOS implements a label with strikethrough