[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 QuartCore framework.

[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]- (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;
}
}[/code]

ref: albertopasca.it

Alberto Pasca