使用CGContext绘图示例

画一条线:

- (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();//取出弹出图形上下文的状态
}


画椭圆:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值