ios 在UIView上画图,线条
生活随笔
收集整理的這篇文章主要介紹了
ios 在UIView上画图,线条
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
1.畫線條(實線,虛線)
- (void)drawRect:(CGRect)rect
{
? ? CGContextRef context = UIGraphicsGetCurrentContext();
? ? [self drawXLine:context rect:rect];
? ? [self drawLegend:context rect:rect];
}
-(CGContextRef)drawXLine:(CGContextRef)context rect:(CGRect)rect {CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//float partren[] = {2,3};//CGContextSetLineDash(context, 0,partren , 2); //畫虛線 CGContextMoveToPoint(context, 10, 0);CGContextAddLineToPoint(context, 0, 100);CGContextStrokePath(context);return context; }2.畫圖例說明
//畫圖例說明 -(void)drawLegend:(CGContextRef)context rect:(CGRect)_rect {CGSize myShadowOffset = CGSizeMake (2, 2);//矩形和陰影的位置 CGContextSaveGState(context);CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1].CGColor);CGContextSetShadow (context, myShadowOffset, 2); //背景的陰影CGContextFillRect(context, CGRectMake(self.frame.size.width/2-100, [UIScreen mainScreen].bounds.size.height-80, 200, 25.0));NSArray *groupTitle = [NSArray arrayWithObjects:@"提出問題數量",@"方案采納數量", nil];int legendCount = [groupTitle count];int stepWidth = 15;for (int i = 0; i < legendCount; i++) {if (i == 0){//設定第一個圖例的顏色CGContextSetFillColorWithColor(context, [UIColor colorWithRed:64.0/255.0 green:104.0/255.0 blue:168.0/255.0 alpha:1.0].CGColor);}else {//設定第二個圖例的顏色CGContextSetFillColorWithColor(context, [UIColor colorWithRed:240.0/255.0 green:152.0/255.0 blue:56.0/255.0 alpha:1.0].CGColor);}CGContextSetShadow (context, myShadowOffset, 1);CGContextFillRect(context, CGRectMake(stepWidth + 50, [UIScreen mainScreen].bounds.size.height-72, 10, 10)); //小方塊的大小以及位置 UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(stepWidth+65, [UIScreen mainScreen].bounds.size.height-76, _rect.size.width, 18)]; //聲明UIlbel并指定其位置和長寬label2.backgroundColor = [UIColor clearColor]; //設置label的背景色,這里設置為透明色。label2.font = [UIFont fontWithName:@"Helvetica-Bold" size:12]; //設置label的字體和字體大小。//label2.transform = CGAffineTransformMakeRotation(0.1); //設置label的旋轉角度label2.text = [groupTitle objectAtIndex:i]; //設置label所顯示的文本label2.textColor = [UIColor blackColor]; //設置文本的顏色label2.textAlignment =NSTextAlignmentLeft; //設置文本在label中顯示的位置,這里為居中。 [self addSubview:label2];stepWidth += 100;}CGContextRestoreGState(context); }?
---恢復內容結束---
轉載于:https://www.cnblogs.com/lihaibo-Leao/p/3260032.html
總結
以上是生活随笔為你收集整理的ios 在UIView上画图,线条的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: drools规则引擎源码解析
- 下一篇: 扒站工具Teleport Pro教程