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];//繪製底部網格線條
}
相關文章
相關標籤/搜索