- (void)drawRect:(CGRect)rect {
// Drawing code
//获取上下文(画笔)
CGContextRefcontext =UIGraphicsGetCurrentContext();
//设置线条宽度
CGContextSetLineWidth(context,5);
//设置线条的颜色
CGContextSetStrokeColorWithColor(context, [UIColorredColor].CGColor);
//1.画线
//[self addLine:context];
//2.一次画多条线
//[self addLines:context];
//3.画空心圆
//[self drawEllipse:context];
//4.画实心圆
//[self drawEllipse2:context];
//5.画矩形
//[self drawRect2:context];
//6.画文字
[selfdrawText:context];
//7.画图片
//[self drawImage];
}
1.画线
- (void)addLine:(CGContextRef)context
{
//从哪个点开始
CGContextMoveToPoint(context,100,100);
//添加另外一个点,连线
CGContextAddLineToPoint(context,50,200);
//添加一个线
CGContextAddLineToPoint(context,150,200);
CGContextAddLineToPoint(context,100,135);
//闭合
CGContextClosePath(context);
//绘画
CGContextStrokePath(context);
}
2.一次画多条线段
- (void)addLines:(CGContextRef)context
{
//从哪个点开始
CGContextMoveToPoint(context,100,100);
CGPointpoints[4] = {CGPointMake(130,100),CGPointMake(200,150),CGPointMake(200,200),CGPointMake(130,120)};
//画多条线
CGContextAddLines(context, points,sizeof(points)/sizeof(points[0]));
//闭合
CGContextClosePath(context);
//画线
CGContextStrokePath(context);
}
3.画空心圆
- (void)drawEllipse:(CGContextRef)context
{
//保存上下文
CGContextSaveGState(context);
//画椭圆
CGContextAddEllipseInRect(context,CGRectMake(10,10,self.bounds.size.width-20,self.bounds.size.height-20));
CGContextStrokePath(context);
//重新设置线条宽度
CGContextSetLineWidth(context,10);
//重新设置线条的颜色
CGContextSetStrokeColorWithColor(context, [UIColorgreenColor].CGColor);
//画椭圆
CGContextAddEllipseInRect(context,CGRectMake(20,20,self.bounds.size.width-40,self.bounds.size.height-40));
CGContextStrokePath(context);
//画椭圆
//恢复到上次保存的画布的状态
CGContextRestoreGState(context);
//画椭圆
CGContextAddEllipseInRect(context,CGRectMake(50,50,self.bounds.size.width-50,self.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(10,10,100,100));
//开始画
CGContextStrokePath(context);
//设置填充的颜色
CGContextSetFillColorWithColor(context, [UIColoryellowColor].CGColor);
//填充区域
CGContextFillRect(context,CGRectMake(200,200,90,30));
}
6.画文字
- (void)drawText:(CGContextRef)context
{
//文字属性
//NSDictionary *attributes = @{NSForegroundColorAttributeName:[]};
//
////画文字
[@"Hello ,xiaoming"drawAtPoint:CGPointMake(20,40)withAttributes:nil];
//设置文字居中显示
NSMutableParagraphStyle*style = [[NSMutableParagraphStylealloc]init];
style.alignment=NSTextAlignmentCenter;
[@"test "drawInRect:CGRectMake(100,100,90,50)withAttributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:20],NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:[UIColoryellowColor]}];
}
7.画图片- (void)drawImage
{
//
[[UIImageimageNamed:@"2_27083_5beb4e628dd4d27"]drawAtPoint:CGPointMake(0,0)];
//[[UIImage imageNamed:@""] drawInRect:
}