// // RectDrawable.m // AntsContract // // Created by Ray on 12/23/16. // Copyright © 2016 United Software Applications, Inc. All rights reserved. // #import "RectDrawable.h" #import "RAUtils.h" @implementation RectDrawable -(instancetype) init:(CGRect) rect color:(NSString*) color { return [self init:rect color:color alpha:1]; } -(instancetype) init:(CGRect) rect color:(NSString*) color alpha:(double) alpha { if (self = [super init]) { // NSMutableDictionary* m1=[drawableTemplate mutableCopy]; // NSMutableDictionary* m2=[drawableTemplate mutableCopy]; // [self setDrawableTemplate:[[RAUtils string2dict:[RAUtils dict2string:drawableTemplate]] mutableCopy] ]; NSMutableDictionary * drawableTemplate = [RectDrawable createRectTemplate:rect color:color alpha:alpha]; [self setDrawableTemplate: [[RAUtils copyDict:drawableTemplate] mutableCopy]]; // [self setDrawableTemplate:[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:drawableTemplate]] ]; } return self; } +(NSMutableDictionary *) createRectTemplate:(CGRect) rect color:(NSString*) color alpha:(double)alpha { NSMutableDictionary* ret=[super createDrawableTemplate:rect]; ret[@"type"]=@"rect" ; ret[@"color"]=color ; ret[@"alpha"]=@(alpha); return ret; } -(void) setDrawableTemplate:(NSMutableDictionary *)drawableTemplate { [super setDrawableTemplate:drawableTemplate]; NSString* colorvalue=drawableTemplate[@"color"]; double alpha=[drawableTemplate[@"alpha"] doubleValue]; if(colorvalue.length>0 ) { unsigned long color = strtoul([colorvalue UTF8String],0,16); self.bgColor= UIColorFromRGB(color); self.alpha = alpha; // [self.bgColor colorWithAlphaComponent:alpha]; } else { self.bgColor= [UIColor blackColor]; self.alpha = 1; } // if(self.color==nil) // self.hAlign=@"center"; // if(self.vAlign==nil) // self.vAlign=@"middle"; } -(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 { CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0]; return parentrect; } -(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 { // [super flipContext:context ContextHeight:flip_height]; CGRect parentrect = [self to_parent_rect:p_rect pos:self.rect offsetX:0 offsetY:0]; CGRect rect= [self parent_to_pdf_rect:parentrect contextHeight:flip_height]; CGContextSaveGState(context); CGContextSetFillColorWithColor(context, [self.bgColor colorWithAlphaComponent:self.alpha].CGColor); // CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0); // // CGContextAddRect(context, parentrect); CGContextFillRect(context, rect); CGContextStrokePath(context); CGContextRestoreGState(context); [self DrawBound:context dataSource:data ParentRect:p_rect startX:x startY:y flipHeight:flip_height range:between_header_and_footer]; return parentrect; // return CGPointMake(parentrect.origin.x+parentrect.size.width, parentrect.origin.y+parentrect.size.height); // // 切换上下文 Core Graphics 和 UIKit 混用 // UIGraphicsPushContext(context); // // // 使用UIKit绘制内容 // [image drawInRect:rect]; // // // 绘制完位图后,再恢复上下文 // UIGraphicsPopContext(); // [super restoreFlip:context ContextHeight:flip_height]; } @end