LineDrawable.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // LineDrawable.m
  3. // pdftest
  4. //
  5. // Created by Ray on 10/15/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "LineDrawable.h"
  9. @implementation LineDrawable
  10. +(NSMutableDictionary *) createlineTemplate:(double) width from:(CGPoint) from to:(CGPoint) to
  11. {
  12. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  13. // "type": "line",
  14. // "x0": 10,
  15. // "y0": 10,
  16. // "x1": 100,
  17. // "y1": 100,
  18. // "line_width": 0.02
  19. ret[@"type"]=@"line" ;
  20. ret[@"x0"]=[NSNumber numberWithDouble:from.x] ;
  21. ret[@"y0"]=[NSNumber numberWithDouble:from.y] ;
  22. ret[@"x1"]=[NSNumber numberWithDouble:to.x] ;
  23. ret[@"y1"]=[NSNumber numberWithDouble:to.y] ;
  24. ret[@"line_width"]=[NSNumber numberWithDouble:width] ;
  25. return ret;
  26. }
  27. -(CGRect) Query_Rect:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer
  28. {
  29. return CGRectMake(0, 0, 0, 0);
  30. }
  31. -(CGRect) Draw:(CGContextRef) context dataSource:(NSMutableDictionary*)data ParentRect:(CGRect)p_rect startX:(double) x startY:(double) y flipHeight:(double)flip_height range:(NSRange)between_header_and_footer
  32. {
  33. CGContextSaveGState(context);
  34. // CGContextSetRGBStrokeColor(context,1,1,1,1.0);//画笔线的颜色
  35. CGContextSetLineWidth(context, [self.drawableTemplate[@"line_width"] doubleValue]);
  36. // CGContextMoveToPoint(context, [self.drawableTemplate[@"x0"] doubleValue], [self.drawableTemplate[@"y0"] doubleValue]);
  37. //
  38. // CGContextAddLineToPoint(context, [self.drawableTemplate[@"x1"] doubleValue], [self.drawableTemplate[@"y1"] doubleValue]);
  39. //
  40. // CGContextStrokePath(context);
  41. CGPoint aPoints[2];//坐标点
  42. aPoints[0] =[self to_pdf_point:p_rect point:CGPointMake([self.drawableTemplate[@"x0"] doubleValue]+x, [self.drawableTemplate[@"y0"] doubleValue]+y) contextHeight:flip_height];//坐标1
  43. aPoints[1] =[self to_pdf_point:p_rect point:CGPointMake([self.drawableTemplate[@"x1"] doubleValue]+x, [self.drawableTemplate[@"y1"] doubleValue]+y) contextHeight:flip_height];//坐标2
  44. DebugLog(@"line from %@ to %@",NSStringFromCGPoint(aPoints[0]),NSStringFromCGPoint(aPoints[1]));
  45. //CGContextAddLines(CGContextRef c, const CGPoint points[],size_t count)
  46. //points[]坐标数组,和count大小
  47. CGContextAddLines(context, aPoints, 2);//添加线
  48. CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径
  49. CGContextRestoreGState(context);
  50. return CGRectMake(0, 0, 0, 0);
  51. }
  52. @end