iOS从绘图开始:DrawRect&CoreText

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

- (void)drawRect:(CGRect)rect {//Drawing code//Quartz 2D绘画环境,一张画布CGContextRef context =UIGraphicsGetCurrentContext();//边框圆 背景圆CGContextSetRGBStrokeColor(context,1,1,0,1.0);//画笔线的颜色CGContextSetLineWidth(context,2.0);//线的宽度//void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度//x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。CGContextAddArc(context,120,120,100,0,2*M_PI,0);//添加一个圆CGContextDrawPath(context, kCGPathStroke);//绘制路径//所占比例圆弧CGContextSetRGBStrokeColor(context,1,0,0,1.0);//画笔线的颜色CGContextSetLineWidth(context,3.0);//线的宽度CGContextAddArc(context,120,120,100, -90* M_PI/180, (endRadius-90) * M_PI/180,0);//添加一个圆CGContextDrawPath(context, kCGPathStroke);//绘制路径//绘制字符串if([[UIDevice currentDevice] systemVersion].floatValue >=7.0) {

NSMutableAttributedString*mabstring =[[NSMutableAttributedString alloc]initWithString:drawText];

[mabstring beginEditing];//对同一段字体进行多属性设置NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithObject:(id)[UIColor redColor].CGColor forKey:(id)kCTForegroundColorAttributeName];//红色字体//设置字体属性CTFontRef font = CTFontCreateWithName(CFSTR("Georgia"),14, NULL);

[attributes setObject:(id)(__bridgeid)font forKey:(id)kCTFontAttributeName];//下划线[mabstring endEditing];//结束编辑[mabstring addAttributes:attributes range:NSMakeRange(0, mabstring.length)];//开始绘制CTFramesetterRef framesetter =CTFramesetterCreateWithAttributedString((CFAttributedStringRef)mabstring);

CGMutablePathRef Path=CGPathCreateMutable();

CGPathAddRect(Path, NULL ,CGRectMake(100, -100,self.bounds.size.width-10, self.bounds.size.height-10));

CTFrameRef frame= CTFramesetterCreateFrame(framesetter, CFRangeMake(0,0), Path, NULL);//压栈,压入图形状态栈中.CGContextSetTextMatrix(context , CGAffineTransformIdentity);//保存现在得上下文图形状态。CGContextSaveGState(context);//x,y轴方向移动CGContextTranslateCTM(context ,0,self.bounds.size.height);//缩放x,y轴方向缩放,代码中坐标系转换是沿x轴翻转180度CGContextScaleCTM(context,1.0,-1.0);

CTFrameDraw(frame,context);//开始绘制大小CGPathRelease(Path);//开始绘制路径CFRelease(framesetter);

}else{//表示开始绘制路径CGContextBeginPath(context);//设置文字大小CGContextSelectFont(context,"Helvetica",20, kCGEncodingMacRoman);

CGContextSetTextDrawingMode(context, kCGTextFill);//设置文本颜色字符为白色CGContextSetRGBFillColor(context,1.0,0.0,1.0,1.0);//白色CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));//绘制文字CGContextShowTextAtPoint(context,20,100, [@"characterAttribute"cStringUsingEncoding:[NSString defaultCStringEncoding]],@"characterAttribute".length);

CGContextStrokePath(context);//表示结束绘制路径CGContextClosePath(context);

}/////////////////////一些字体属性设置//////////////////////////[mabstring addAttribute:(id)kCTFontAttributeName value:(__bridge id)font range:NSMakeRange(0, 4)];//[attributes setObject:(id)[NSNumber numberWithInt:kCTUnderlineStyleDouble] forKey:(id)kCTUnderlineStyleAttributeName];//下划线//[mabstring addAttributes:attributes range:NSMakeRange(0, 4)];//设置属性的文字范围//设置斜体字//CTFontRef font = CTFontCreateWithName((CFStringRef)[UIFont italicSystemFontOfSize:20].fontName, 14, NULL);//[mabstring addAttribute:(id)kCTFontAttributeName value:(__bridge id)(font) range:NSMakeRange(0, 4)];}

上面的代码,是在UIView上绘制一个圆形比例图和文字。

下面是CoreText在Label上的使用。

1UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60,2, sectionHeader.bounds.size.width-60-15-10,56)];2titleLabel.font = [UIFont systemFontOfSize:17.0f];3[sectionHeader addSubview:titleLabel];4NSString *titleStr = [NSString stringWithFormat:@"你还有%ld未通过", courseCount];5NSInteger courseCount =[obligatoryCourseArray count];67//设置标签文字8NSMutableAttributedString *attrituteString =[[NSMutableAttributedString alloc] initWithString:titleStr];9//设置标签文字的属性10[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:25]} range:NSMakeRange(3, [NSString stringWithFormat:@"%ld", courseCount].length)];11//显示在Label上12titleLabel.attributedText = attrituteString;

当然Label上绘制的Text的属性可以用上面的在UIView上绘制中的方法。

再来看一个UIImageView的:

1{23UIImageView *obligatoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,30, tableView.bounds.size.width, ([UIScreen mainScreen].bounds.size.height-20-44)/3-30*2)];4[cell addSubview:obligatoryImageView];5obligatoryImageView.backgroundColor =[UIColor whiteColor];6UIGraphicsBeginImageContext(obligatoryImageView.bounds.size);7[obligatoryImageView.image drawInRect:CGRectMake(0,0, obligatoryImageView.bounds.size.width, obligatoryImageView.bounds.size.height)];8CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCaPRound);//边缘样式9CGContextSetLineWidth(UIGraphicsGetCurrentContext(),0.2);//线宽10CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);11CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),153/255.0,153/255.0,153/255.0,1.0);//颜色12CGContextBeginPath(UIGraphicsGetCurrentContext());13CGContextMoveToPoint(UIGraphicsGetCurrentContext(),35,5);//起点坐标14CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35,5);//终点坐标15CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-5));16CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),35, (obligatoryImageView.bounds.size.height-5));17CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),35,5);1819CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),35, (obligatoryImageView.bounds.size.height-5)/5+5);20CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-5)/5+5);2122CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-5)/5*2+5);23CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),35, (obligatoryImageView.bounds.size.height-10)/5*2+5);2425CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),35, (obligatoryImageView.bounds.size.height-10)/5*3+5);26CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-10)/5*3+5);2728CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), self.view.bounds.size.width-35, (obligatoryImageView.bounds.size.height-10)/5*4+5);29CGContextAddLineToPoint(UIGraphicsGetCurrentContext(),35, (obligatoryImageView.bounds.size.height-10)/5*4+5);3031CGContextStrokePath(UIGraphicsGetCurrentContext());32obligatoryImageView.image=UIGraphicsGetImageFromCurrentImageContext();33UIGraphicsEndImageContext();3435NSArray *obligatoryArray =[courseArray objectAtIndex:indexPath.row];3637UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, tableView.bounds.size.width,30)];38titleLabel.textColor =[UIColor murpTextLowColor];39titleLabel.textAlignment =NSTextAlignmentCenter;40titleLabel.font = [UIFont systemFontOfSize:15.0f];41titleLabel.text = [[obligatoryArray objectAtIndex:0] objectForKey:@"Extend2"];42[cell addSubview:titleLabel];4344for(inti =0; i <=5; i ++) {4546UILabel *left1 = [[UILabel alloc] initWithFrame:CGRectMake(0,30+(obligatoryImageView.bounds.size.height-10)/5*i,35,10)];47left1.textColor =[UIColor murpTextLowColor];48left1.textAlignment =NSTextAlignmentRight;49left1.font = [UIFont systemFontOfSize:12.0f];50left1.text = [NSString stringWithFormat:@"%d%%",100-20*i];51[cell addSubview:left1];5253if(i < obligatoryArray.count-1) {5455UILabel *down = [[UILabel alloc] initWithFrame:CGRectMake(35+(tableView.bounds.size.width-35*2)/5*i, ([UIScreen mainScreen].bounds.size.height-20-44)/3-30, (tableView.bounds.size.width-35*2)/5,30)];56down.textColor =[UIColor murpTextLowColor];57down.textAlignment =NSTextAlignmentCenter;58down.font = [UIFont systemFontOfSize:12.0f];59[cell addSubview:down];60down.text = [NSString stringWithFormat:@"%@分",[[obligatoryArray objectAtIndex:i+1] objectForKey:@"Extend1"]];6162CGFloat heights = [[[obligatoryArray objectAtIndex:i+1] objectForKey:@"Extend3"] floatValue]/100.0*(obligatoryImageView.bounds.size.height-5*2);63UIImageView *heightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(35+(tableView.bounds.size.width-35*2)/5*i+5, (([UIScreen mainScreen].bounds.size.height-20-44)/3-30-5)-heights, (tableView.bounds.size.width-35*2)/5-10, heights)];64heightImageView.backgroundColor =[UIColor murpThemeGreenColor];65[cell addSubview:heightImageView];6667UILabel *heightsLabel = [[UILabel alloc] initWithFrame:CGRectMake(35+(tableView.bounds.size.width-35*2)/5*i+5, (([UIScreen mainScreen].bounds.size.height-20-44)/3-30-5)-heights-20, (tableView.bounds.size.width-35*2)/5-10,20)];68heightsLabel.textColor =[UIColor murpTextLowColor];69heightsLabel.textAlignment =NSTextAlignmentCenter;70heightsLabel.font = [UIFont systemFontOfSize:10.0f];71[cell addSubview:heightsLabel];72heightsLabel.text = [[obligatoryArray objectAtIndex:i+1] objectForKey:@"Extend2"];73}74}75}

其实我也不知道想说什么,哟嘿,最后看个效果吧:

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航