| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- //
- // 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
|