RectDrawable.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // RectDrawable.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/23/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RectDrawable.h"
  9. @implementation RectDrawable
  10. -(instancetype) init:(CGRect) rect color:(NSString*) color
  11. {
  12. if (self = [super init]) {
  13. // NSMutableDictionary* m1=[drawableTemplate mutableCopy];
  14. // NSMutableDictionary* m2=[drawableTemplate mutableCopy];
  15. // [self setDrawableTemplate:[[RAUtils string2dict:[RAUtils dict2string:drawableTemplate]] mutableCopy] ];
  16. NSMutableDictionary * drawableTemplate = [RectDrawable createRectTemplate:rect color:color];
  17. [self setDrawableTemplate:[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:drawableTemplate]] ];
  18. }
  19. return self;
  20. }
  21. +(NSMutableDictionary *) createRectTemplate:(CGRect) rect color:(NSString*) color
  22. {
  23. NSMutableDictionary* ret=[super createDrawableTemplate:rect];
  24. ret[@"type"]=@"rect" ;
  25. ret[@"color"]=color ;
  26. return ret;
  27. }
  28. -(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate
  29. {
  30. [super setDrawableTemplate:drawableTemplate];
  31. NSString* colorvalue=drawableTemplate[@"color"];
  32. if(colorvalue.length>0 )
  33. {
  34. unsigned long color = strtoul([colorvalue UTF8String],0,16);
  35. self.bgColor= UIColorFromRGB(color);
  36. }
  37. else
  38. {
  39. self.bgColor= [UIColor blackColor];
  40. }
  41. // if(self.color==nil)
  42. // self.hAlign=@"center";
  43. // if(self.vAlign==nil)
  44. // self.vAlign=@"middle";
  45. }
  46. -(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
  47. {
  48. CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  49. return parentrect;
  50. }
  51. -(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
  52. {
  53. // [super flipContext:context ContextHeight:flip_height];
  54. CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0];
  55. CGRect rect= [self parent_to_pdf_rect:parentrect contextHeight:flip_height];
  56. CGContextSaveGState(context);
  57. CGContextSetFillColorWithColor(context, self.bgColor.CGColor);
  58. // CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
  59. //
  60. // CGContextAddRect(context, parentrect);
  61. CGContextFillRect(context, rect);
  62. CGContextStrokePath(context);
  63. CGContextRestoreGState(context);
  64. [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer];
  65. return parentrect;
  66. // return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height);
  67. // // 切换上下文 Core Graphics 和 UIKit 混用
  68. // UIGraphicsPushContext(context);
  69. //
  70. // // 使用UIKit绘制内容
  71. // [image drawInRect:rect];
  72. //
  73. // // 绘制完位图后,再恢复上下文
  74. // UIGraphicsPopContext();
  75. // [super restoreFlip:context ContextHeight:flip_height];
  76. }
  77. @end