iOS · UILabel加删除线

创建自定义子类DeleteLineLabel,继承自UILabel,然后在自定义子类DeleteLineLabel中

 

方法一(上下文):

 

 1 - (void)drawRect:(CGRect)rect {
 2     [super drawRect:rect];
 3 
 4     CGContextRef ref = UIGraphicsGetCurrentContext();
 5     
 6     //绘制起点
 7     CGContextMoveToPoint(ref, 0, rect.size.height * 0.5);
 8     //绘制终点
 9     CGContextAddLineToPoint(ref, rect.size.width, rect.size.height * 0.5);
10     //完成绘制
11     CGContextStrokePath(ref);
12 
13     
14 }

 

方法二(画矩形):

 

 1 - (void)drawRect:(CGRect)rect {
 2     // 调用super的drawRect:方法,会按照父类绘制label的文字
 3     [super drawRect:rect];
 4     
 5     // 取文字的颜色作为删除线的颜色(同文字颜色,可不写)
 6     [self.textColor set];
 7 
 8     // 绘制(找到label的中间位置)
 9     UIRectFill(CGRectMake(0, rect.size.height * 0.5, rect.size.width, 1));
10     
11 }

 

 

完成效果:

 

 

posted @ 2016-12-27 22:17  Accepted.DXY  阅读(665)  评论(0编辑  收藏  举报