IOS 用drawRect 画表格

自定义一个View DrawLine

DrawLine.h

#import <UIKit/UIKit.h>

@protocol gridTouchDelete <NSObject>

- (void)gridTouchColumn:(NSInteger)column touchRow:(NSInteger)row;

@end

@interface DrawLine : UIView

@property(nonatomic, assign) id<gridTouchDelete>delegate;

@end

#import "DrawLine.h"

#define GRID_WIDTH 44
#define GRID_HEIGHT 44

@implementation DrawLine

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 格子线条颜色
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
    CGContextBeginPath(context);
    
    NSInteger numRows = 5;
    float gridHeight = numRows*(GRID_HEIGHT+2)+1;
    
    // 背景色
    CGRect rectangleGrid = CGRectMake(0,0,self.frame.size.width,gridHeight);
    CGContextAddRect(context, rectangleGrid);
    CGContextSetFillColorWithColor(context, [[UIColor grayColor] colorWithAlphaComponent:0.2].CGColor);
    CGContextFillPath(context);
    
    for (int i = 0; i < 7; i++) {
        //columns 列
        CGContextMoveToPoint(context, i*(GRID_WIDTH+1)+i*1, 0);
        CGContextAddLineToPoint(context, i*(GRID_WIDTH+1)+i*1, gridHeight);
        
        if (i > numRows) continue;
        
        //rows 行
        CGContextMoveToPoint(context, 0, i*(GRID_HEIGHT+1)+i*1+1);
        CGContextAddLineToPoint(context, self.frame.size.width, 0+i*(GRID_HEIGHT+1)+i*1+1);
    
    }

    CGContextStrokePath(context);
    CGContextSetAllowsAntialiasing(context, YES);
    
    NSInteger gridNum = numRows * 7;
    for (int i = 0; i < gridNum; i ++) {
        int targetColumn = i%7;
        int targetRow = i/7;
        int targetX = targetColumn * (GRID_WIDTH+2)+15;
        int targetY =  targetRow * (GRID_HEIGHT+2)+15;
        
         NSString *gridStr = [NSString stringWithFormat:@"%d",i+1];
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
            UIColor *fontColor;
            if (targetColumn == 0 || targetColumn == 6) {
                fontColor = [UIColor redColor];
            }
            else
            {
                fontColor = [UIColor blackColor];
            }
            
            [gridStr drawInRect:CGRectMake(targetX, targetY, self.frame.size.width, GRID_WIDTH) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:17],NSForegroundColorAttributeName:fontColor}];
        }
        else
        {
            [gridStr drawInRect:CGRectMake(targetX, targetY, self.frame.size.width, GRID_WIDTH) withFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:17] lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter];
            
            CGContextSetFillColorWithColor(context,[UIColor redColor].CGColor);
        }
       
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    
    
    float xLocation = touchPoint.x;
    float yLocation = touchPoint.y;
    
    int column = floorf(xLocation/(GRID_HEIGHT+2));
    int row = floorf(yLocation/(GRID_WIDTH+2));
    
    if ([_delegate respondsToSelector:@selector(gridTouchColumn:touchRow:)]) {
        [_delegate gridTouchColumn:column touchRow:row];
    }
}

MainViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    DrawLine *lineView = [[DrawLine alloc]initWithFrame:CGRectMake(0, 84, self.view.frame.size.width, 600)];
    lineView.backgroundColor = [UIColor clearColor];
    lineView.delegate = self;
    [self.view addSubview:lineView];
}

- (void)gridTouchColumn:(NSInteger)column touchRow:(NSInteger)row
{
    NSLog(@"行:%d,列:%d",column,row);
}

原文地址:https://www.cnblogs.com/joesen/p/3964336.html