首先,获取上下文
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);