RectDrawable.m 3.9 KB

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