// 覆盖drawRect方法,你可以在此自定义绘画和动画
- (void)drawRect:(CGRect)rect
{
//An opaque type that represents a Quartz 2D drawing environment.
//一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画
CGContextRef context = UIGraphicsGetCurrentContext();
UIFont *font = [UIFont fontWithName:@"PingFang HK" size:30.0f];
NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
[[UIColor brownColor] set]; //设置上下文使用的颜色
CGContextSetLineWidth(context, 2.0f);
CGContextMoveToPoint(context, 2.0f, 200.0f); // 画笔移动到某点
CGContextAddLineToPoint(context, [UIScreen mainScreen].bounds.size.width-4, 200.0f);
CGContextAddLineToPoint(context, [UIScreen mainScreen].bounds.size.width-4, 400.0f);
CGContextAddLineToPoint(context, 2.f, 400.0f);
CGContextClosePath(context);//闭合矩形
for (int i = 1; i<200/50; i++) {
CGContextMoveToPoint(context, 2.0f, 200.0 +50*i); // 画笔移动到某点
CGContextAddLineToPoint(context, [UIScreen mainScreen].bounds.size.width-4, 200.0 +50*i);
NSString *tempStr =[NSString stringWithFormat:@"项目%d",i];
CGFloat height = [self calculateCellHeight:tempStr fontSize:30 maxWidth:100];//字体高
[[NSString stringWithFormat:@"项目%d",i] drawAtPoint:CGPointMake(5.0f, 200.0 +50*i + (50 - height)/2) withAttributes:dic];
}
int count = (int)([UIScreen mainScreen].bounds.size.width-4)/100;
for (int i = 1; i<count; i++) {
CGContextMoveToPoint(context, 2.0f + 100*i, 200.0 ); // 画笔移动到某点
CGContextAddLineToPoint(context, 2.0f + 100*i, 400.0f );
}
CGContextStrokePath(context); //执行绘制
//颜色填充
CGRect strokeRect = CGRectMake( 2.0f, 200.0f, [UIScreen mainScreen].bounds.size.width-5, 49);
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:0.5 blue:1 alpha:0.5].CGColor);
CGContextFillRect(context,strokeRect);
}