IOS绘制一个简单的表格

#pragma mark 绘制直线
- (void) drawLine : (CGContextRef) context {
    
    //绘制底部表格横线  行高40
    for (int i = 0; i < 7; i++){
        //设置开始位置
        CGContextMoveToPoint(context, 30, 40+40*i);
        //设置结束位置
        CGContextAddLineToPoint(context, WIDTH - 10, 40+40*i);
        
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(5, 20+40*i, 20, 40)];
        lab.textColor = [UIColor whiteColor];
        lab.textAlignment = NSTextAlignmentRight;
        lab.text = [NSString stringWithFormat:@"%d",180-20*i];
        lab.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
        [self addSubview:lab];
        
    }
    CGFloat jiange = (WIDTH - 40)/7.0;//行宽
    //绘制底部表格竖线
    for (int i = 0; i < 8; i++){
        //设置开始位置
        CGContextMoveToPoint(context, 30 + jiange*i, 0);
        //设置结束位置
        CGContextAddLineToPoint(context, 30 + jiange*i, 307);
    }
    [[UIColor whiteColor] setStroke];//设置线条颜色
    CGContextSetLineWidth(context, 1);//设置线条宽度
    CGContextDrawPath(context, kCGPathFillStroke);
}
#pragma mark 绘制底部背景色
- (void)drawBackgroundColoc:(CGContextRef) context{
    CGContextAddRect(context,self.bounds);//绘制矩形
    CGContextSetFillColorWithColor(context, HEXCOLOR(0xEF5B5B).CGColor);//填充颜色
    [HEXCOLOR(0xEF5B5B) setStroke];//设置线条颜色
    CGContextSetLineWidth(context, 1);//设置线条宽度
    CGContextDrawPath(context, kCGPathFillStroke);
}

- (void)drawRect:(CGRect)rect {
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self drawBackgroundColoc:context];//绘制底部背景色
    [self drawLine:context];//绘制底部网格线条
}

猜你喜欢

转载自my.oschina.net/u/2287505/blog/1632146