您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页让UILabel自带中划线,当要显示打折价格时

让UILabel自带中划线,当要显示打折价格时

来源:二三娱乐

继承UILabel

.h

@interface LineLabel : UILabel

@property(nonatomic, assign) BOOL showLine;
@property(nonatomic, assign) UIColor * showLineColor;

@end

.m

#import "LineLabel.h"
@implementation LineLabel

//添加中划线或者是下划线或者任意位置的横线(自己调整)
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    if (_showLine) {
        if (_showLineColor) {
            [_showLineColor set];  //横线的颜色设置
        }else {
            [[UIColor blackColor] set]; //默认 横线颜色
        }
        
        CGContextRef c = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(c, 1);
        CGContextBeginPath(c);
        CGFloat halfWayUp = rect.size.height/2 + rect.origin.y;
        CGContextMoveToPoint(c, rect.origin.x, halfWayUp);//起点
        CGContextAddLineToPoint(c, rect.origin.x + rect.size.width, halfWayUp);//终点
        CGContextStrokePath(c);
    }
}


@end

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务