画线、简单图形总结(draw)

一、描绘数字

   CGContextRef context = UIGraphicsGetCurrentContext(); //画布

   CGContextSaveGState(context);

    NSString* text = @"15";

    NSString* fontname = @"Helvetica";

    CGContextSelectFont(context, [fontname UTF8String], 18.0, kCGEncodingMacRoman);

    [[UIColorblueColor] setFill];

    CGContextSetShouldAntialias(context, true);//让字体渲染比较清晰提高画质以使之柔和

    CGContextSetTextDrawingMode(context, kCGTextFill);

    //kCGTextClip kCGTextInvisible这样中间没数字 kCGTextFill kCGTextFillClip有数字

    //stroke描边kCGTextFillStroke kCGTextFillStrokeClip数字黑白相映

    // kCGTextStroke kCGTextStrokeClip数字为空心的黑边

    CGContextSetTextMatrix (context, CGAffineTransformMake(1, 0, 0, -1, 0, 0));

    //从文本空间到用户控件的转换矩阵删除的话数字是倒放的

    CGContextShowTextAtPoint(context, 10.0f,

                             20.0f,

                             [text cStringUsingEncoding:NSASCIIStringEncoding], text.length); //绘制文本

    CGContextRestoreGState(context);

二、画圆

static inline void drawArc(CGContextRef ctx, CGPoint point, UIColor* color, NSInteger radius) {//画圆

    CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));

    CGContextFillEllipseInRect(ctx, CGRectMake(point.x - radius, point.y - radius, radius * 2, radius * 2));

}//比下面通过扇形画要快

三、画扇形

#define radians(x) ((x)*M_PI / 180.0)

static inline void drawArc(CGContextRef ctx, CGPoint point, float angle_start, float angle_end, UIColor* color) {

    CGContextMoveToPoint(ctx, point.x, point.y);

    CGContextSetFillColor(ctx, CGColorGetComponents( [color CGColor]));

    CGContextAddArc(ctx, point.x, point.y, radius,  angle_start, angle_end, 0);

    //CGContextClosePath(ctx);

    CGContextFillPath(ctx);

}

 CGContextRef context = UIGraphicsGetCurrentContext(); //画布

CGPoint point = CGPointMake(20, 20);

 float angle_start = radians(0.0);

 float angle_end = radians(360.0);

drawArc(context, point, angle_start, angle_end, [UIColor redColor]);

 四、画线

CGContextRef context = UIGraphicsGetCurrentContext(); //画布        

CGContextSetRGBStrokeColor(context, 204.0/255.0, 102.0/255.0, 0.0/255.0, 1.0); //笔色

CGContextSetLineWidth(context, 0.8); //线宽

CGContextMoveToPoint(context, 0.0, 0.0);//起始点

CGContextAddLineToPoint(context, 0.0, 100.0);//终点

CGContextStrokePath(context);//画

五、画矩形

 CGContextRef context = UIGraphicsGetCurrentContext(); //画布

 CGRect bgRect = CGRectMake(batchCodeLabelWidth + ballHeigth * k , ballHeigth * i, ballHeigth, ballHeigth);//区域

  CGContextSetRGBFillColor(context, 241.0/255.0, 241.0/255.0, 241.0/255.0, 1);//颜色

   CGContextAddRect(context, bgRect);

   CGContextDrawPath(context, kCGPathFillStroke);

六、画汉字

直接使用NSString的方法:

[@"哈哈"  drawAtPoint:CGPointMake(110, 35) withFont:[UIFontsystemFontOfSize:14]];

原文地址:https://www.cnblogs.com/swallow37/p/2846000.html