画一条线:
- (void)drawContextLine
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
CGContextMoveToPoint(context, 20, 200);
CGContextAddLineToPoint(context, 200,200);
CGContextStrokePath(context);//根据路径绘制
// CGContextDrawPath(context, kCGPathStroke);根据绘图模式绘制路径
}
写文字:
- (void)drawContextText
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);//线条宽度
CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5);//颜色
UIFont *font = [UIFont boldSystemFontOfSize:18.0];
[@"公司:北京中软科技股份有限公司\n部门:ERP事业部\n姓名:McLiang" drawInRect:CGRectMake(20, 40, 280, 300) withFont:font];
}
画一个正方形,没有边框:
- (void)drawContextRect1
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5);//颜色
CGContextFillRect(context, CGRectMake(2, 2, 270, 270));//设置矩形
CGContextStrokePath(context);
}
- (void)drawContextRect2
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
CGContextSetLineWidth(context, 2.0);
CGContextAddRect(context, CGRectMake(2, 2, 270, 270));//添加矩形边框路径
CGContextStrokePath(context);
}
- (void)drawContextRectBackground//背景颜色
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0f, self.bounds.size.height);//坐标平移变换
CGContextScaleCTM(context, 1.0f, -1.0f);//坐标缩放变换
UIGraphicsPushContext(context);//压入保存当前图形上下文的状态
CGContextSetLineWidth(context,320);
CGContextSetRGBStrokeColor(context, 250.0/255, 250.0/255, 210.0/255, 1.0);
CGContextStrokeRect(context, CGRectMake(0, 0, 320, 460));
UIGraphicsPopContext();//取出弹出图形上下文的状态
}