美文网首页
在价格上面画线的小demo

在价格上面画线的小demo

作者: MONKEY小巧 | 来源:发表于2016-05-05 13:44 被阅读25次

在做商品降价的时候,有时候会遇到给lable添加一条删除线的问题,此时有两张实现方法:一种是画两个点,再连起来;另一种是苹果封装好的方法--UIRectFill

不管怎样,都要自定义lable,在.m文件中重写drawLine:方法:

  • 方法二:
    - (void)drawRect:(CGRect)rect {
// 调用super的目的, 保留之前绘制的文字
[super drawRect:rect];
// 获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 起点
CGFloat startX = 0 + rect.origin.x;
CGFloat startY = rect.size.height * 0.5 + rect.origin.y;
CGContextMoveToPoint(ctx, startX, startY);
// 终点
CGFloat endX = rect.size.width + rect.origin.x;
CGFloat endY = startY;
CGContextAddLineToPoint(ctx, endX, endY);
// 绘图渲染
CGContextStrokePath(ctx);

}

  • 方法二:
    - (void)drawRect:(CGRect)rect {
// 调用super的目的, 保留之前绘制的文字
[super drawRect:rect];
CGFloat x = 0 + rect.origin.x;
CGFloat y = rect.size.height * 0.5 + rect.origin.y;
CGFloat w = rect.size.width;
CGFloat h = 1;
UIRectFill(CGRectMake(x, y, w, h));

}

相关文章

网友评论

      本文标题:在价格上面画线的小demo

      本文链接:https://www.haomeiwen.com/subject/hiedrttx.html