CGContextRef类画图形、文字、图片

- (void)drawRect:(CGRect)rect {

    // Drawing code

        

    //获取上下文(画笔)

    CGContextRef context = UIGraphicsGetCurrentContext();

    //设置线条宽度

    CGContextSetLineWidth(context, 5);

    //设置线条的颜色

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);    

   

    //1.画线

//    [self addLine:context];

    //2.一次画多条线

//    [self addLines:context];

    //3.画空心圆

//    [self drawEllipse:context];

    //4.画实心圆

//    [self drawEllipse2:context];

    //5.画矩形

//    [self drawRect2:context];

    //6.画文字

    [self drawText:context];

    //7.画图片

//    [self drawImage];


}



 1.画线

- (void)addLine:(CGContextRef)context

{

    //从哪个点开始

    CGContextMoveToPoint(context, 100100);

    

    //添加另外一个点,连线

    CGContextAddLineToPoint(context, 50200);

    //添加一个线

    CGContextAddLineToPoint(context, 150200);

    

    CGContextAddLineToPoint(context, 100135);

    

    //闭合

    CGContextClosePath(context);

    //绘画

    CGContextStrokePath(context);

}


 2.一次画多条线段

- (void)addLines:(CGContextRef)context

{

    //从哪个点开始

    CGContextMoveToPoint(context, 100100);

    

    CGPoint points[4] = {CGPointMake(130100),CGPointMake(200,150),CGPointMake(200200),CGPointMake(130120)};

    

    //画多条线

    CGContextAddLines(context, points,sizeof(points)/sizeof(points[0]));

    //闭合

    CGContextClosePath(context);

    

    //画线

    CGContextStrokePath(context);

}


 3.画空心圆

- (void)drawEllipse:(CGContextRef)context

{

    //保存上下文

    CGContextSaveGState(context);

    

    //画椭圆

    CGContextAddEllipseInRect(context, CGRectMake(1010,self.bounds.size.width 20self.bounds.size.height 20));

    CGContextStrokePath(context);

    

    //重新设置线条宽度

    CGContextSetLineWidth(context, 10);

    //重新设置线条的颜色

    CGContextSetStrokeColorWithColor(context, [UIColorgreenColor].CGColor);

    

    //画椭圆

    CGContextAddEllipseInRect(context, CGRectMake(2020,self.bounds.size.width 40self.bounds.size.height 40));

    CGContextStrokePath(context);

    

    //画椭圆

    //恢复到上次保存的画布的状态

    CGContextRestoreGState(context);

    

    //画椭圆

    CGContextAddEllipseInRect(context, CGRectMake(5050,self.bounds.size.width 50self.bounds.size.height 50));

    CGContextStrokePath(context);

}


 4.画实心圆

- (void)drawEllipse2:(CGContextRef)context

{

    //设置填充的颜色

    CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);

    

    //画填充的圆

    CGContextFillEllipseInRect(context,CGRectMake(10, 10, self.frame.size.width - 20, self.frame.size.height - 20));

}


 5.画矩形

- (void)drawRect2:(CGContextRef)context

{

    //画矩形

    CGContextAddRect(context, CGRectMake(1010100100));

    //开始画

    CGContextStrokePath(context);

    

    //设置填充的颜色

    CGContextSetFillColorWithColor(context, [UIColoryellowColor].CGColor);

    //填充区域

    CGContextFillRect(context, CGRectMake(2002009030));

}


 6.画文字

- (void)drawText:(CGContextRef)context

{

    //文字属性

    //    NSDictionary *attributes = @{NSForegroundColorAttributeName:[]};

    //

    //    //画文字

    [@"Hello ,xiaoming" drawAtPoint:CGPointMake(2040)withAttributes:nil];


    

    

    //设置文字居中显示

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyleallocinit];

    style.alignment NSTextAlignmentCenter;

    

    [@"test " drawInRect:CGRectMake(1001009050)withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:20],NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:[UIColor yellowColor]}];

}


 7.画图片- (void)drawImage

{

    //

    [[UIImage imageNamed:@"2_27083_5beb4e628dd4d27"]drawAtPoint:CGPointMake(00)];

    

    //[[UIImage imageNamed:@""] drawInRect:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值