SimpleGrid.m 15 KB

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