SimpleGrid.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. //
  2. // SimpleGride.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 14-3-4.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "SimpleGrid.h"
  9. //#import "Constant.h"
  10. #define COLOR(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
  11. @implementation SimpleGrid
  12. @synthesize griddelegate;
  13. - (id)initWithFrame:(CGRect)frame
  14. {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. // Initialization code
  18. }
  19. return self;
  20. }
  21. -(NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim
  22. {
  23. NSScanner *theScanner = [NSScanner scannerWithString:html];
  24. NSString *text = nil;
  25. while ([theScanner isAtEnd] == NO) {
  26. // find start of tag
  27. [theScanner scanUpToString:@"<" intoString:NULL] ;
  28. // find end of tag
  29. [theScanner scanUpToString:@">" intoString:&text] ;
  30. // replace the found tag with a space
  31. //(you can filter multi-spaces out later if you wish)
  32. html = [html stringByReplacingOccurrencesOfString:
  33. [ NSString stringWithFormat:@"%@>", text]
  34. withString:@""];
  35. }
  36. return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
  37. }
  38. -(void) initgrid //:(PageData*) pageData
  39. {
  40. for(UIView* v in self.subviews)
  41. {
  42. if(v.tag==200)// do not remove refresh control;
  43. continue;
  44. [v removeFromSuperview];
  45. }
  46. int column_count=[[self.header valueForKey:@"count"] intValue];
  47. int row_count = [[self.data valueForKey:@"count"] intValue];
  48. self.contentSize = CGSizeMake(0.0,0.0);
  49. // self.hrefs = [[NSMutableDictionary alloc] init];
  50. // self.pageData = pageData;
  51. self.hidden = TRUE;
  52. // self.basetag = row_count*column_count;
  53. self.row_count = row_count;
  54. self.column_count = column_count;
  55. // self.fullrowselect = true;
  56. self.top_line = 2;
  57. self.left_line =2;
  58. self.right_line =2;
  59. self.bottom_line=2;
  60. self.line = 1;
  61. int columns_width[column_count] ;
  62. int row_offset = 20;
  63. int header_offset = 20;
  64. // int padding_top = 2;
  65. // int padding_bottom = 2;
  66. int background_margin = 15;
  67. int padding_h = 5;
  68. // int padding_v = 5;
  69. UILabel * compute_item = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0 )] ;
  70. for(int i=0;i<self.column_count;i++)
  71. {
  72. NSDictionary *header_column_json = [self.header objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  73. NSString* aname = [header_column_json valueForKey:@"aname"];
  74. compute_item.text = aname;
  75. [compute_item sizeToFit];
  76. columns_width[i] = compute_item.frame.size.width+2*padding_h;
  77. header_offset = compute_item.frame.size.height;
  78. }
  79. for(int i=0;i<self.row_count;i++)
  80. {
  81. NSDictionary *objrecord = [self.data objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  82. for(int j=0;j<self.column_count;j++)
  83. {
  84. NSString* val = [objrecord valueForKey:[NSString stringWithFormat:@"val_%d",j]];
  85. if([val isEqual:[NSNull null]])
  86. val=@"";
  87. if(val==nil)
  88. val=@"";
  89. if([val isEqualToString:@"null"])
  90. val=@"";
  91. NSString* text=[self flattenHTML:val trimWhiteSpace:true];
  92. compute_item.text = text;
  93. [compute_item sizeToFit];
  94. if(columns_width[j]<compute_item.frame.size.width+2*padding_h)
  95. columns_width[j] = compute_item.frame.size.width+2*padding_h;
  96. row_offset = compute_item.frame.size.height;
  97. }
  98. }
  99. int background_width = 0;
  100. for(int i=0;i<self.column_count;i++)
  101. {
  102. background_width+=columns_width[i];
  103. }
  104. background_width+=self.left_line;
  105. background_width+=self.right_line;
  106. background_width+=self.line*(self.column_count-1);
  107. long background_height= row_offset*self.row_count;
  108. background_height+=self.top_line;
  109. background_height+=self.bottom_line;
  110. background_height+=self.line*(self.row_count-1);
  111. background_height+=header_offset;
  112. UIView* backgroudView = [[UIView alloc] initWithFrame:CGRectMake(0,0, background_width, background_height )] ;
  113. backgroudView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];//[UIColor blueColor];
  114. backgroudView.alpha=1;
  115. int line_offset_row=0;
  116. int column_offset = 0;
  117. int line_offset_column=0;
  118. for(int j=0;j<self.column_count;j++)
  119. {
  120. if(j==0)
  121. line_offset_column+=self.left_line;
  122. else
  123. line_offset_column += self.line;
  124. TouchLabel * item = [[TouchLabel alloc] initWithFrame:CGRectMake(column_offset+line_offset_column, self.top_line, columns_width[j], row_offset ) ] ;
  125. NSDictionary *header_column_json = [self.header objectForKey:[NSString stringWithFormat:@"item_%d",j]];
  126. NSString* aname = [header_column_json valueForKey:@"aname"];
  127. item.text= aname;//[NSString stringWithFormat:@"item text %d",i*i+j*j];
  128. //item.insets=UIEdgeInsetsMake(0, padding_h, 0, 0);
  129. item.textAlignment = NSTextAlignmentCenter;
  130. item.tag = j;
  131. // if([self.hrefs valueForKey:item.text]!=nil)
  132. // item.textColor =[UIColor blueColor];
  133. // UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LabelLong:)];
  134. // longPress.minimumPressDuration = 0.8; //定义按的时间
  135. // [item addGestureRecognizer:longPress];
  136. //
  137. // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LabelTap:)];
  138. // // tap.minimumPressDuration = 0.8; //定义按的时间
  139. // [item addGestureRecognizer:tap];
  140. //
  141. item.backgroundColor = [UIColor lightGrayColor];
  142. // DebugLog(@"size after fit: {x=%f , y=%f}",item.frame.size.width,item.frame.size.height);
  143. column_offset += columns_width[j];
  144. [backgroudView addSubview:item];
  145. }
  146. for(int i=0;i<self.row_count;i++)
  147. {
  148. line_offset_row+= self.line;
  149. int column_offset = 0;
  150. int line_offset_column=0;
  151. NSDictionary *objrecord = [self.data objectForKey:[NSString stringWithFormat:@"item_%d",i]];
  152. for(int j=0;j<self.column_count;j++)
  153. {
  154. if(j==0)
  155. line_offset_column+=self.left_line;
  156. else
  157. line_offset_column += self.line;
  158. NSString* val = [objrecord valueForKey:[NSString stringWithFormat:@"val_%d",j]];
  159. TouchLabel * item = [[TouchLabel alloc] initWithFrame:CGRectMake(column_offset+line_offset_column, header_offset+row_offset*i+line_offset_row, columns_width[j], row_offset ) ] ;
  160. item.text= val;//[NSString stringWithFormat:@"item text %d",i*i+j*j];
  161. item.insets=UIEdgeInsetsMake(0, padding_h, 0, 0);
  162. item.textAlignment = NSTextAlignmentLeft;
  163. item.tag =i*self.column_count+j;
  164. // if([self.hrefs valueForKey:item.text]!=nil)
  165. // item.textColor =[UIColor blueColor];
  166. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LabelLong:)];
  167. longPress.minimumPressDuration = 0.8; //定义按的时间
  168. [item addGestureRecognizer:longPress];
  169. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LabelTap:)];
  170. // tap.minimumPressDuration = 0.8; //定义按的时间
  171. [item addGestureRecognizer:tap];
  172. if(i%2==1)
  173. item.backgroundColor = [UIColor whiteColor];
  174. else
  175. item.backgroundColor = COLOR(238,238,238,1.0);
  176. // DebugLog(@"size after fit: {x=%f , y=%f}",item.frame.size.width,item.frame.size.height);
  177. column_offset += columns_width[j];
  178. [backgroudView addSubview:item];
  179. }
  180. }
  181. CGSize contentSize;
  182. contentSize.height = self.superview.frame.size.height;//background_height+2*background_margin;
  183. contentSize.width = background_width+2*background_margin;
  184. self.contentSize = contentSize;
  185. // DebugLog(@"background window : {x=%f , y=%f , width=%f , height=%f }",backgroudView.frame.origin.x,backgroudView.frame.origin.y ,backgroudView.frame.size.width,backgroudView.frame.size.height);
  186. [self addSubview:backgroudView];
  187. self.hidden = false;
  188. }
  189. #pragma mark - Label Gesture
  190. - (void)drawSelect:(CGRect)rect
  191. {
  192. DebugLog(@"drawSelect");
  193. CGContextRef context = UIGraphicsGetCurrentContext();
  194. // CGRect rectt = CGRectMake(100 ,100, 100, 100);//坐标
  195. CGContextSetRGBFillColor(context, 0/ 255.0f, 0/ 255.0f, 0/ 255.0f,0.2);//颜色(RGB),透明度
  196. CGContextFillRect(context, rect);
  197. // Drawing code
  198. // CGContextRef context = UIGraphicsGetCurrentContext();
  199. //
  200. // CGContextSetLineWidth(context, 2.0);
  201. //
  202. // CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
  203. //
  204. // CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
  205. //
  206. // CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
  207. //
  208. // CGContextStrokePath(context);
  209. }
  210. -(void)LabelLong:(UILongPressGestureRecognizer*)gestureRecognizer{
  211. if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
  212. DebugLog(@"长按事件");
  213. // if(gestureRecognizer.view.tag - self.basetag < MAX_COLUMN) //header
  214. // return;
  215. // CGRect rect;
  216. // if(self.fullrowselect)
  217. // {
  218. //
  219. // rect.origin.x= gestureRecognizer.view.superview.frame.origin.x+self.left_line;
  220. // rect.origin.y= gestureRecognizer.view.superview.frame.origin.y+self.left_line;
  221. // }
  222. //int idx = [t locationInView:self].y / d.cellHeight;
  223. [UIView beginAnimations:nil context:nil];
  224. [UIView setAnimationDuration:0.65];
  225. long row = gestureRecognizer.view.tag/self.column_count;
  226. long column = gestureRecognizer.view.tag%self.column_count;
  227. if(self.fullrowselect)
  228. {
  229. for(int i=0;i<self.column_count;i++)
  230. {
  231. [self viewWithTag:(row*self.column_count+i)].alpha = 0.5;
  232. }
  233. for(int i=0;i<self.column_count;i++)
  234. {
  235. [self viewWithTag:(row*self.column_count+i)].alpha = 1.0;
  236. }
  237. }
  238. else
  239. {
  240. gestureRecognizer.view.alpha=0.5;
  241. gestureRecognizer.view.alpha=1;
  242. }
  243. // for(int i=0;i<[d.dataSource.titles count];i++){
  244. // UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * d.cellHeight + i + 1000];
  245. // l.alpha = .5;
  246. // }
  247. // for(int i=0;i<[d.dataSource.titles count];i++){
  248. // UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * d.cellHeight + i + 1000];
  249. // l.alpha = 1.0;
  250. // }
  251. [UIView commitAnimations];
  252. if(self.fullrowselect)
  253. {
  254. if (self.griddelegate && [self.griddelegate respondsToSelector:@selector(LongPress:row:)])
  255. [griddelegate LongPress:gestureRecognizer.view row:row];
  256. }
  257. else
  258. if (self.griddelegate && [self.griddelegate respondsToSelector:@selector(LongPress:row:column:)])
  259. [griddelegate LongPress:gestureRecognizer.view row:row column:column];
  260. // [griddelegate LongPress:gestureRecognizer.view row:row];
  261. // [griddelegate LongPress:gestureRecognizer.view _id:[self.pageData get_id:row-1]];//first row is header;
  262. }
  263. }
  264. -(void)LabelTap:(UILongPressGestureRecognizer*)gestureRecognizer {
  265. DebugLog(@"LabelTap tag = %ld",(long)gestureRecognizer.view.tag);
  266. // if(gestureRecognizer.view.tag - self.basetag < MAX_COLUMN) //header
  267. // return;
  268. [UIView beginAnimations:nil context:nil];
  269. [UIView setAnimationDuration:0.65];
  270. long row = gestureRecognizer.view.tag/self.column_count;
  271. long column = gestureRecognizer.view.tag%self.column_count;
  272. if(self.fullrowselect)
  273. {
  274. for(int i=0;i<self.column_count;i++)
  275. {
  276. [self viewWithTag:(row*self.column_count+i)].alpha = 0.5;
  277. }
  278. for(int i=0;i<self.column_count;i++)
  279. {
  280. [self viewWithTag:(row*self.column_count+i)].alpha = 1.0;
  281. }
  282. }
  283. else
  284. {
  285. gestureRecognizer.view.alpha=0.5;
  286. gestureRecognizer.view.alpha=1;
  287. }
  288. [UIView commitAnimations];
  289. if(self.fullrowselect)
  290. {
  291. if (self.griddelegate && [self.griddelegate respondsToSelector:@selector(Tap:row:)])
  292. [griddelegate Tap:gestureRecognizer.view row:row];
  293. }
  294. else
  295. if (self.griddelegate && [self.griddelegate respondsToSelector:@selector(Tap:row:column:)])
  296. [griddelegate Tap:gestureRecognizer.view row:row column:column];
  297. // TouchLabel* label = (TouchLabel*)gestureRecognizer.view;
  298. //
  299. //
  300. //
  301. //
  302. //
  303. // NSString * url =[self.hrefs valueForKey:label.text];
  304. // if(url!=nil)
  305. // {
  306. // label.textColor = [UIColor purpleColor];
  307. // [griddelegate Tap:label url:url];//first row is header;
  308. // }
  309. }
  310. /*
  311. // Only override drawRect: if you perform custom drawing.
  312. // An empty implementation adversely affects performance during animation.
  313. - (void)drawRect:(CGRect)rect
  314. {
  315. // Drawing code
  316. }
  317. */
  318. @end