CGContextRef类画图形、文字、图片

来源:互联网 发布:thinkpad推荐 知乎 编辑:程序博客网 时间:2024/05/16 14:18

- (void)drawRect:(CGRect)rect {

   // Drawing code

       

   //获取上下文(画笔)

   CGContextRef context =UIGraphicsGetCurrentContext();

   //设置线条宽度

   CGContextSetLineWidth(context, 5);

   //设置线条的颜色

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

   

   //1.画线

//    [self addLine:context];

   //2.一次画多条线

//    [selfaddLines:context];

   //3.画空心圆

//    [selfdrawEllipse:context];

   //4.画实心圆

//    [selfdrawEllipse2:context];

   //5.画矩形

//    [selfdrawRect2:context];

   //6.画文字

   [selfdrawText: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,[UIColororangeColor].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)];

    

   //[[UIImageimageNamed:@""] drawInRect:

0 0