http://my.oschina.net/LangZiAiFer/blog/175623
在iPhone屏幕上画长方形,直线和文字
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置颜色,仅填充 4 条边CGContextSetStrokeColorWithColor(ctx, [[UIColor colorWithRed:1 green:1 blue:1 alpha:0.5] CGColor]);
// 设置线宽为 1
CGContextSetLineWidth(ctx, 1.0);
// 设置长方形 4 个顶点
CGPoint poins[] = {CGPointMake(5, 5),CGPointMake(425, 5),CGPointMake(425, 125),CGPointMake(5, 125)};
CGContextAddLines(ctx,poins,4);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
首先,获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
画无框矩形
- //设置矩形填充颜色:红色
- CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
- //填充矩形
- CGContextFillRect(context, rect);
- //执行绘画
- CGContextStrokePath(context);
画有框矩形
- //设置矩形填充颜色:红色
- CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
- //填充矩形
- CGContextFillRect(context, rect);
- //设置画笔颜色:黑色
- CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
- //设置画笔线条粗细
- CGContextSetLineWidth(context, 1.0);
- //画矩形边框
- CGContextAddRect(context,rect);
- //执行绘画
- CGContextStrokePath(context);
画文字
- //设置画笔线条粗细
- CGContextSetLineWidth(context, 1.0);
- //设置矩形填充颜色:红色
- CGContextSetRGBFillColor (context, 1.0, 0.0, 0.0, 1.0);
- //设置字体
- UIFont *font = [UIFont boldSystemFontOfSize:31.0];
- //在指定的矩形区域内画文字
- [text drawInRect:rect withFont:font];
画线
- //设置画笔线条粗细
- CGContextSetLineWidth(context, 5.0);
- //设置线条样式
- CGContextSetLineCap(context, kCGLineCapButt);
- //设置画笔颜色:黑色
- CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
- //画点连线
- CGContextAddLines(context, points, count);
- //执行绘画
- CGContextStrokePath(context);