绘制过程:
设置颜色 - (void)set
设置线条宽度 - (void)CGContextSetLineWidth (CGContextRef c,CGFloat width);
设置连线类型 - (void)CGContextSetLineJoin (CGContextRef c,CGLineJoin join); //默认连线类型 kCGLineJoinMiter
设置线条起始点 -void CGContextMoveToPoint (CGContextRef c,CGFloat x,CGFloat y);
设置线条终点 -void CGContextAddLineToPoint (CGContextRef c,CGFloat x,CGFloat y);
开始绘制 -void CGContextStrokePath (CGContextRef c);
- (void)drawRect:(CGRect)rect{
[[UIColor brownColor] set]; // - (void)set
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext,5.0f);
/* Start point */
CGContextMoveToPoint(currentContext,50.0f,10.0f);
/* end point */
CGContextAddLineToPoint(currentContext,100.0f,200.0f);
/* Extend the line to another point 绘制简单连线 (默认连线类型 kCGLineJoinMiter) */
CGContextAddLineToPoint(currentContext,300.0f,100.0f);
/* draw the line */
CGContextStrokePath(currentContext);
}
2.连线类型
enum CGLineJoin {
kCGLineJoinMiter, //默认类型
kCGLineJoinRound,
kCGLineJoinBevel
};
- (void) drawRooftopAtTopPointof:(CGPoint)paramTopPoint
textToDisplay:(NSString *)paramText
lineJoin:(CGLineJoin)paramLineJoin{
[[UIColor brownColor] set];
CGContextRef currentContext = UIGraphicsGetCurrentContext();
/* Set the line join 设置连线类型 */
CGContextSetLineJoin(currentContext,paramLineJoin);
CGContextSetLineWidth(currentContext,20.0f);
/* Start point */
CGContextMoveToPoint(currentContext,
paramTopPoint.x - 140,
paramTopPoint.y + 100);
/* end point */
CGContextAddLineToPoint(currentContext,
paramTopPoint.x,
paramTopPoint.y);
/* Extend the line to another point to make the rooftop */
CGContextAddLineToPoint(currentContext,
paramTopPoint.x + 140,
paramTopPoint.y + 100);
/* draw the lines */
CGContextStrokePath(currentContext);
[[UIColor blackColor] set];
/* Now draw the text */
CGPoint drawingPoint = CGPointMake(paramTopPoint.x - 40.0f,
paramTopPoint.y + 60.0f);
UIFont *font = [UIFont boldSystemFontOfSize:30.0f];
[paramText drawAtPoint:drawingPoint
withAttributes:@{NSFontAttributeName : font}];
}
- (void)drawRect:(CGRect)rect{
[self drawRooftopAtTopPointof:CGPointMake(160.0f, 40.0f)
textToDisplay:@"Miter"
lineJoin:kCGLineJoinMiter];
[self drawRooftopAtTopPointof:CGPointMake(160.0f, 180.0f)
textToDisplay:@"Bevel"
lineJoin:kCGLineJoinBevel];
[self drawRooftopAtTopPointof:CGPointMake(160.0f, 320.0f)
textToDisplay:@"Round"
lineJoin:kCGLineJoinRound];
}