iOS通过Quartz画矩形、文字、线

来源:互联网 发布:初中数学题库软件 编辑:程序博客网 时间:2024/04/28 00:20
首先,获取上下文

CGContextRef context = UIGraphicsGetCurrentContext();


画无框矩形

  1. //设置矩形填充颜色:红色  
  2. CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);  
  3. //填充矩形  
  4. CGContextFillRect(context, rect);  
  5. //执行绘画  
  6. CGContextStrokePath(context);  
 

画有框矩形

  1. //设置矩形填充颜色:红色  
  2. CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);  
  3. //填充矩形  
  4. CGContextFillRect(context, rect);  
  5. //设置画笔颜色:黑色  
  6. CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);  
  7. //设置画笔线条粗细  
  8. CGContextSetLineWidth(context, 1.0);  
  9. //画矩形边框  
  10. CGContextAddRect(context,rect);  
  11. //执行绘画  
  12. CGContextStrokePath(context);  

 

画文字

  1. //设置画笔线条粗细  
  2. CGContextSetLineWidth(context, 1.0);  
  3. //设置矩形填充颜色:红色  
  4. CGContextSetRGBFillColor (context, 1.0, 0.0, 0.0, 1.0);  
  5. //设置字体  
  6. UIFont *font = [UIFont boldSystemFontOfSize:31.0];  
  7. //在指定的矩形区域内画文字  
  8. [text drawInRect:rect withFont:font];  

 

画线

  1. //设置画笔线条粗细  
  2. CGContextSetLineWidth(context, 5.0);  
  3. //设置线条样式  
  4. CGContextSetLineCap(context, kCGLineCapButt);  
  5. //设置画笔颜色:黑色  
  6. CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);  
  7. //画点连线  
  8. CGContextAddLines(context, points, count);  
  9. //执行绘画  
  10. CGContextStrokePath(context);  
原创粉丝点击