SimpleGrid.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. self.contentSize = CGSizeMake(0.0,0.0);
  47. self.hrefs = [[NSMutableDictionary alloc] init];
  48. self.pageData = pageData;
  49. self.hidden = TRUE;
  50. self.basetag = MAX_ROW*MAX_COLUMN;
  51. self.row_count = [pageData get_count]+1;
  52. self.column_count = self.header_aname.count;
  53. // self.fullrowselect = true;
  54. self.top_line = 2;
  55. self.left_line =2;
  56. self.right_line =2;
  57. self.bottom_line=2;
  58. self.line = 1;
  59. int columns_width[MAX_COLUMN] ;
  60. for(int i=0;i<MAX_COLUMN;i++)
  61. {
  62. columns_width[i] = 10;
  63. }
  64. int row_offset = 20;
  65. // int padding_top = 2;
  66. // int padding_bottom = 2;
  67. int background_margin = 15;
  68. int padding_h = 10;
  69. int padding_v = 5;
  70. // int button_padding = 20;
  71. // NSArray *columns_width = NsArray alloca(<#size_t#>)
  72. // UILabel * compute_item = [UILabel alloc];
  73. // TouchLabel * compute_item = [[TouchLabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0 )] ;
  74. UILabel * compute_item = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0 )] ;
  75. NSMutableArray* values = [[NSMutableArray alloc] init];
  76. for(int i=0;i<self.column_count;i++)
  77. {
  78. [values addObject:self.header_aname[i]];
  79. // NSString* s=[self.header valueForKey:[self.header allKeys][i]];
  80. // [values addObject:[self.header_aname[i]];
  81. // [values setValue:[self.header valueForKey:[self.header allKeys][i]] forKey:nil];
  82. }
  83. for(int i=0;i<self.row_count-1;i++)
  84. {
  85. NSDictionary *objrecord = [pageData.records objectForKey:[NSString stringWithFormat:@"record%d",i]];
  86. // NSArray* keys= [objrecord allKeys];
  87. for(int j=0;j<self.column_count;j++)
  88. {
  89. // NSString* name =self.header_name[j];
  90. NSString* val = [objrecord valueForKey:self.header_name[j]];
  91. if([val isEqual:[NSNull null]])
  92. val=@"";
  93. if(val==nil)
  94. val=@"";
  95. if([val isEqualToString:@"null"])
  96. val=@"";
  97. NSString* text=[self flattenHTML:val trimWhiteSpace:true];
  98. [values addObject:text];
  99. if(! [text isEqualToString:val])
  100. {
  101. NSScanner *theScanner = [NSScanner scannerWithString:val];
  102. NSString *href = nil;
  103. // find start of tag
  104. [theScanner scanUpToString:@"https://" intoString:NULL] ;
  105. // find end of tag
  106. [theScanner scanUpToString:@"'" intoString:&href] ;
  107. [self.hrefs setObject:href forKey:text];
  108. // int i=0;
  109. }
  110. }
  111. // for(int j=0;j<keys.count;j++)
  112. // {
  113. // if([self.header valueForKey:keys[j]]!=nil)
  114. // {
  115. // }
  116. // }
  117. }
  118. for(int i=0;i<self.row_count;i++)
  119. {
  120. // int column_offset = 0;
  121. for(int j=0;j<self.column_count;j++)
  122. {
  123. // int idx =i*self.column_count+j;
  124. // NSString* val=values[i*self.column_count+j];
  125. compute_item.text= values[i*self.column_count+j];//[NSString stringWithFormat:@"item text %d",i*i+j*j];
  126. if(i==0)
  127. compute_item.textAlignment = NSTextAlignmentCenter;
  128. else
  129. compute_item.textAlignment = NSTextAlignmentLeft;
  130. // DebugLog(@"size: {x=%f , y=%f}",compute_item.frame.size.width,compute_item.frame.size.height);
  131. [compute_item sizeToFit];
  132. if(i==0&&j==0)
  133. {
  134. //row height only need compute once;
  135. row_offset = compute_item.frame.size.height+2*padding_v;
  136. }
  137. // DebugLog(@"size after fit: {x=%f , y=%f}",compute_item.frame.size.width,compute_item.frame.size.height);
  138. if(compute_item.frame.size.width+2*padding_h>columns_width[j])
  139. columns_width[j]=compute_item.frame.size.width+padding_h;
  140. }
  141. }
  142. int background_width = 0;
  143. for(int i=0;i<self.column_count;i++)
  144. {
  145. background_width+=columns_width[i];
  146. }
  147. background_width+=self.left_line;
  148. background_width+=self.right_line;
  149. background_width+=self.line*(self.column_count-1);
  150. long background_height= row_offset*self.row_count;
  151. background_height+=self.top_line;
  152. background_height+=self.bottom_line;
  153. background_height+=self.line*(self.row_count-1);
  154. UIView* backgroudView = [[UIView alloc] initWithFrame:CGRectMake(/*background_margin, background_margin,*/0,0, background_width, background_height )] ;
  155. backgroudView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];//[UIColor blueColor];
  156. backgroudView.alpha=1;
  157. int line_offset_row=0;
  158. for(int i=0;i<self.row_count;i++)
  159. {
  160. if(i==0)
  161. line_offset_row+=self.top_line;
  162. else
  163. line_offset_row+= self.line;
  164. int column_offset = 0;
  165. int line_offset_column=0;
  166. for(int j=0;j<self.column_count;j++)
  167. {
  168. if(j==0)
  169. line_offset_column+=self.left_line;
  170. else
  171. line_offset_column += self.line;
  172. TouchLabel * item = [[TouchLabel alloc] initWithFrame:CGRectMake(column_offset+line_offset_column, row_offset*i+line_offset_row, columns_width[j], row_offset ) ] ;
  173. item.text= values[i*self.column_count+j];//[NSString stringWithFormat:@"item text %d",i*i+j*j];
  174. if(i==0)
  175. compute_item.textAlignment = NSTextAlignmentCenter;
  176. else
  177. compute_item.textAlignment = NSTextAlignmentLeft;
  178. item.tag = self.basetag + MAX_ROW*i+j;
  179. if([self.hrefs valueForKey:item.text]!=nil)
  180. item.textColor =[UIColor blueColor];
  181. UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LabelLong:)];
  182. longPress.minimumPressDuration = 0.8; //定义按的时间
  183. [item addGestureRecognizer:longPress];
  184. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LabelTap:)];
  185. // tap.minimumPressDuration = 0.8; //定义按的时间
  186. [item addGestureRecognizer:tap];
  187. if(i>0)
  188. {
  189. if(i%2==1)
  190. item.backgroundColor = [UIColor whiteColor];
  191. else
  192. item.backgroundColor = COLOR(238,238,238,1.0);
  193. }
  194. else
  195. item.backgroundColor = [UIColor lightGrayColor];
  196. // DebugLog(@"size after fit: {x=%f , y=%f}",item.frame.size.width,item.frame.size.height);
  197. column_offset += columns_width[j];
  198. [backgroudView addSubview:item];
  199. }
  200. }
  201. CGSize contentSize;
  202. contentSize.height = self.superview.frame.size.height;//background_height+2*background_margin;
  203. contentSize.width = background_width+2*background_margin;
  204. self.contentSize = contentSize;
  205. 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);
  206. [self addSubview:backgroudView];
  207. self.hidden = false;
  208. }
  209. #pragma mark - Label Gesture
  210. - (void)drawSelect:(CGRect)rect
  211. {
  212. DebugLog(@"drawSelect");
  213. CGContextRef context = UIGraphicsGetCurrentContext();
  214. // CGRect rectt = CGRectMake(100 ,100, 100, 100);//坐标
  215. CGContextSetRGBFillColor(context, 0/ 255.0f, 0/ 255.0f, 0/ 255.0f,0.2);//颜色(RGB),透明度
  216. CGContextFillRect(context, rect);
  217. // Drawing code
  218. // CGContextRef context = UIGraphicsGetCurrentContext();
  219. //
  220. // CGContextSetLineWidth(context, 2.0);
  221. //
  222. // CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
  223. //
  224. // CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
  225. //
  226. // CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
  227. //
  228. // CGContextStrokePath(context);
  229. }
  230. -(void)LabelLong:(UILongPressGestureRecognizer*)gestureRecognizer{
  231. if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
  232. DebugLog(@"长按事件");
  233. if(gestureRecognizer.view.tag - self.basetag < MAX_COLUMN) //header
  234. return;
  235. // CGRect rect;
  236. // if(self.fullrowselect)
  237. // {
  238. //
  239. // rect.origin.x= gestureRecognizer.view.superview.frame.origin.x+self.left_line;
  240. // rect.origin.y= gestureRecognizer.view.superview.frame.origin.y+self.left_line;
  241. // }
  242. //int idx = [t locationInView:self].y / d.cellHeight;
  243. [UIView beginAnimations:nil context:nil];
  244. [UIView setAnimationDuration:0.65];
  245. long row = (gestureRecognizer.view.tag-self.basetag) /100;
  246. if(self.fullrowselect)
  247. {
  248. for(int i=0;i<self.column_count;i++)
  249. {
  250. [self viewWithTag:(self.basetag+row*MAX_COLUMN+i)].alpha = 0.5;
  251. }
  252. for(int i=0;i<self.column_count;i++)
  253. {
  254. [self viewWithTag:(self.basetag+row*MAX_COLUMN+i)].alpha = 1.0;
  255. }
  256. }
  257. else
  258. {
  259. gestureRecognizer.view.alpha=0.5;
  260. gestureRecognizer.view.alpha=1;
  261. }
  262. // for(int i=0;i<[d.dataSource.titles count];i++){
  263. // UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * d.cellHeight + i + 1000];
  264. // l.alpha = .5;
  265. // }
  266. // for(int i=0;i<[d.dataSource.titles count];i++){
  267. // UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * d.cellHeight + i + 1000];
  268. // l.alpha = 1.0;
  269. // }
  270. [UIView commitAnimations];
  271. [griddelegate LongPress:gestureRecognizer.view _id:[self.pageData get_id:row-1]];//first row is header;
  272. // [delegate gestureRecognizer.view _id:[self.pageData get_id:row]];
  273. // [delegate touchLabel:self touchesWtihTag:self.tag];
  274. // [self drawRect : gestureRecognizer.view.frame ];
  275. // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息" message:@"确定删除该模式吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
  276. // [alert show];
  277. }
  278. }
  279. -(void)LabelTap:(UILongPressGestureRecognizer*)gestureRecognizer {
  280. DebugLog(@"LabelTap tag = %ld",(long)gestureRecognizer.view.tag);
  281. if(gestureRecognizer.view.tag - self.basetag < MAX_COLUMN) //header
  282. return;
  283. TouchLabel* label = (TouchLabel*)gestureRecognizer.view;
  284. NSString * url =[self.hrefs valueForKey:label.text];
  285. if(url!=nil)
  286. {
  287. label.textColor = [UIColor purpleColor];
  288. [griddelegate Tap:label url:url];//first row is header;
  289. }
  290. //- (void) Tap:(UIView*)view url:(NSString*) url
  291. // if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
  292. // DebugLog(@"LabelTap");
  293. //// UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"消息" message:@"确定删除该模式吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
  294. //// [alert show];
  295. // }
  296. }
  297. /*
  298. // Only override drawRect: if you perform custom drawing.
  299. // An empty implementation adversely affects performance during animation.
  300. - (void)drawRect:(CGRect)rect
  301. {
  302. // Drawing code
  303. }
  304. */
  305. @end