[Objc] Draw matrix on UIView

A simple memo to draw a matrix on a view, based on view size.

objectivec draw matrix

You need only to subclass a view and add QuartzCore framework.

-(void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();

	// set line Color
	UIColor *lineColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
	CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
	CGContextSetLineWidth(context, 1.0);

	CGFloat gridSize = 60.0f;

	// draw horizontal lines
	CGFloat linePosition = 0.0;
	while(linePosition < rect.size.height)
	{
		CGContextBeginPath(context);
		CGContextMoveToPoint(context, 0, linePosition);
		CGContextAddLineToPoint(context, rect.size.width, linePosition);
		CGContextStrokePath(context);
		linePosition += gridSize;
	}

	// draw vertical lines
	linePosition = 0.0f;
	while (linePosition < rect.size.width)
	{
		CGContextBeginPath(context);
		CGContextMoveToPoint(context, linePosition, 0);
		CGContextAddLineToPoint(context, linePosition, rect.size.height);
		CGContextStrokePath(context);
		linePosition += gridSize;
	}
}

ref: albertopasca.it

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.