| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- //
- // SimpleGride.m
- // Apex Mobile
- //
- // Created by Ray on 14-3-4.
- // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
- //
- #import "SimpleGrid.h"
- //#import "Constant.h"
- #import "RAUtils.h"
- #define COLOR(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
- @implementation SimpleGrid
- @synthesize griddelegate;
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
-
- // Initialization code
- }
- return self;
- }
- -(NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim
- {
- NSScanner *theScanner = [NSScanner scannerWithString:html];
- NSString *text = nil;
-
- while ([theScanner isAtEnd] == NO) {
- // find start of tag
- [theScanner scanUpToString:@"<" intoString:NULL] ;
- // find end of tag
- [theScanner scanUpToString:@">" intoString:&text] ;
- // replace the found tag with a space
- //(you can filter multi-spaces out later if you wish)
- html = [html stringByReplacingOccurrencesOfString:
- [ NSString stringWithFormat:@"%@>", text]
- withString:@""];
- }
-
- return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
- }
- -(void) initgrid //:(PageData*) pageData
- {
- for(UIView* v in self.subviews)
- {
- if(v.tag==200)// do not remove refresh control;
- continue;
- [v removeFromSuperview];
-
- }
-
- int column_count=[[self.header valueForKey:@"count"] intValue];
- int row_count = [[self.data valueForKey:@"count"] intValue];
- self.contentSize = CGSizeMake(0.0,0.0);
- // self.hrefs = [[NSMutableDictionary alloc] init];
- // self.pageData = pageData;
- self.hidden = TRUE;
- // self.basetag = row_count*column_count;
- self.row_count = row_count;
- self.column_count = column_count;
- // self.fullrowselect = true;
-
- self.top_line = 2;
- self.left_line =2;
- self.right_line =2;
- self.bottom_line=2;
- self.line = 1;
-
- int columns_width[column_count] ;
-
-
- int row_offset = 20;
- int header_offset = 20;
- // int padding_top = 2;
- // int padding_bottom = 2;
- int background_margin = 15;
-
-
-
- int padding_h = 5;
- // int padding_v = 5;
-
- UILabel * compute_item = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0 )] ;
-
- for(int i=0;i<self.column_count;i++)
- {
-
- NSDictionary *header_column_json = [self.header objectForKey:[NSString stringWithFormat:@"item_%d",i]];
-
- NSString* aname = [header_column_json valueForKey:@"aname"];
- compute_item.text = aname;
- [compute_item sizeToFit];
- columns_width[i] = compute_item.frame.size.width+2*padding_h;
-
- header_offset = compute_item.frame.size.height;
-
-
-
- }
-
- for(int i=0;i<self.row_count;i++)
- {
- NSDictionary *objrecord = [self.data objectForKey:[NSString stringWithFormat:@"item_%d",i]];
-
-
- for(int j=0;j<self.column_count;j++)
- {
-
- NSString* val = [objrecord valueForKey:[NSString stringWithFormat:@"val_%d",j]];
- if([val isEqual:[NSNull null]])
- val=@"";
- if(val==nil)
- val=@"";
- if([val isEqualToString:@"null"])
- val=@"";
- NSString* text=[self flattenHTML:val trimWhiteSpace:true];
-
-
- compute_item.text = text;
- [compute_item sizeToFit];
- if(columns_width[j]<compute_item.frame.size.width+2*padding_h)
- columns_width[j] = compute_item.frame.size.width+2*padding_h;
-
-
- row_offset = compute_item.frame.size.height;
-
-
-
- }
-
-
- }
-
-
-
-
- int background_width = 0;
- for(int i=0;i<self.column_count;i++)
- {
- background_width+=columns_width[i];
-
- }
- background_width+=self.left_line;
- background_width+=self.right_line;
- background_width+=self.line*(self.column_count-1);
-
-
- long background_height= row_offset*self.row_count;
- background_height+=self.top_line;
- background_height+=self.bottom_line;
- background_height+=self.line*(self.row_count-1);
- background_height+=header_offset;
-
- UIView* backgroudView = [[UIView alloc] initWithFrame:CGRectMake(0,0, background_width, background_height )] ;
- backgroudView.backgroundColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];//[UIColor blueColor];
- backgroudView.alpha=1;
-
- int line_offset_row=0;
- int column_offset = 0;
- int line_offset_column=0;
- for(int j=0;j<self.column_count;j++)
- {
-
- if(j==0)
- line_offset_column+=self.left_line;
- else
- line_offset_column += self.line;
-
-
- TouchLabel * item = [[TouchLabel alloc] initWithFrame:CGRectMake(column_offset+line_offset_column, self.top_line, columns_width[j], row_offset ) ] ;
-
- NSDictionary *header_column_json = [self.header objectForKey:[NSString stringWithFormat:@"item_%d",j]];
-
- NSString* aname = [header_column_json valueForKey:@"aname"];
- item.text= aname;//[NSString stringWithFormat:@"item text %d",i*i+j*j];
- //item.insets=UIEdgeInsetsMake(0, padding_h, 0, 0);
- item.textAlignment = NSTextAlignmentCenter;
- item.tag = j;
-
- // if([self.hrefs valueForKey:item.text]!=nil)
- // item.textColor =[UIColor blueColor];
-
- // UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LabelLong:)];
- // longPress.minimumPressDuration = 0.8; //定义按的时间
- // [item addGestureRecognizer:longPress];
- //
- // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LabelTap:)];
- // // tap.minimumPressDuration = 0.8; //定义按的时间
- // [item addGestureRecognizer:tap];
- //
-
-
- item.backgroundColor = [UIColor lightGrayColor];
-
-
-
- // DebugLog(@"size after fit: {x=%f , y=%f}",item.frame.size.width,item.frame.size.height);
- column_offset += columns_width[j];
-
-
- [backgroudView addSubview:item];
-
-
-
-
- }
-
-
- for(int i=0;i<self.row_count;i++)
- {
-
- line_offset_row+= self.line;
-
- int column_offset = 0;
- int line_offset_column=0;
-
-
- NSDictionary *objrecord = [self.data objectForKey:[NSString stringWithFormat:@"item_%d",i]];
-
-
- for(int j=0;j<self.column_count;j++)
- {
-
- if(j==0)
- line_offset_column+=self.left_line;
- else
- line_offset_column += self.line;
- NSString* val = [objrecord valueForKey:[NSString stringWithFormat:@"val_%d",j]];
-
- TouchLabel * item = [[TouchLabel alloc] initWithFrame:CGRectMake(column_offset+line_offset_column, header_offset+row_offset*i+line_offset_row, columns_width[j], row_offset ) ] ;
- item.text= val;//[NSString stringWithFormat:@"item text %d",i*i+j*j];
-
- item.insets=UIEdgeInsetsMake(0, padding_h, 0, 0);
-
- item.textAlignment = NSTextAlignmentLeft;
- item.tag =i*self.column_count+j;
-
- // if([self.hrefs valueForKey:item.text]!=nil)
- // item.textColor =[UIColor blueColor];
-
- UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LabelLong:)];
- longPress.minimumPressDuration = 0.8; //定义按的时间
- [item addGestureRecognizer:longPress];
-
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LabelTap:)];
- // tap.minimumPressDuration = 0.8; //定义按的时间
- [item addGestureRecognizer:tap];
-
-
-
- if(i%2==1)
- item.backgroundColor = [UIColor whiteColor];
- else
- item.backgroundColor = COLOR(238,238,238,1.0);
-
-
-
-
- // DebugLog(@"size after fit: {x=%f , y=%f}",item.frame.size.width,item.frame.size.height);
- column_offset += columns_width[j];
-
-
- [backgroudView addSubview:item];
-
-
-
-
- }
- }
-
- CGSize contentSize;
- contentSize.height = self.superview.frame.size.height;//background_height+2*background_margin;
- contentSize.width = background_width+2*background_margin;
- self.contentSize = contentSize;
-
- // 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);
- [self addSubview:backgroudView];
- self.hidden = false;
-
- }
- #pragma mark - Label Gesture
- - (void)drawSelect:(CGRect)rect
- {
-
- DebugLog(@"drawSelect");
- CGContextRef context = UIGraphicsGetCurrentContext();
- // CGRect rectt = CGRectMake(100 ,100, 100, 100);//坐标
- CGContextSetRGBFillColor(context, 0/ 255.0f, 0/ 255.0f, 0/ 255.0f,0.2);//颜色(RGB),透明度
- CGContextFillRect(context, rect);
- // Drawing code
-
- // CGContextRef context = UIGraphicsGetCurrentContext();
- //
- // CGContextSetLineWidth(context, 2.0);
- //
- // CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);
- //
- // CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
- //
- // CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
- //
- // CGContextStrokePath(context);
-
- }
- -(void)LabelLong:(UILongPressGestureRecognizer*)gestureRecognizer{
-
- if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
- DebugLog(@"长按事件");
- // if(gestureRecognizer.view.tag - self.basetag < MAX_COLUMN) //header
- // return;
- // CGRect rect;
- // if(self.fullrowselect)
- // {
- //
- // rect.origin.x= gestureRecognizer.view.superview.frame.origin.x+self.left_line;
- // rect.origin.y= gestureRecognizer.view.superview.frame.origin.y+self.left_line;
- // }
- //int idx = [t locationInView:self].y / d.cellHeight;
- // [UIView beginAnimations:nil context:nil];
- // [UIView setAnimationDuration:0.65];
- long row = gestureRecognizer.view.tag/self.column_count;
- long column = gestureRecognizer.view.tag%self.column_count;
- [UIView animateWithDuration:0.65 animations:^{
-
- if(self.fullrowselect)
- {
-
- for(int i=0;i<self.column_count;i++)
- {
- [self viewWithTag:(row*self.column_count+i)].alpha = 0.5;
- }
- for(int i=0;i<self.column_count;i++)
- {
- [self viewWithTag:(row*self.column_count+i)].alpha = 1.0;
- }
- }
- else
- {
- gestureRecognizer.view.alpha=0.5;
- gestureRecognizer.view.alpha=1;
- }
- }];
-
- // for(int i=0;i<[d.dataSource.titles count];i++){
- // UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * d.cellHeight + i + 1000];
- // l.alpha = .5;
- // }
- // for(int i=0;i<[d.dataSource.titles count];i++){
- // UILabel *l = (UILabel*)[dataGridComponent viewWithTag:idx * d.cellHeight + i + 1000];
- // l.alpha = 1.0;
- // }
- // [UIView commitAnimations];
-
- if(self.fullrowselect)
- {
- if (self.griddelegate && [self.griddelegate respondsToSelector:@selector(LongPress:row:)])
- [griddelegate LongPress:gestureRecognizer.view row:row];
- }
- else
- if (self.griddelegate && [self.griddelegate respondsToSelector:@selector(LongPress:row:column:)])
- [griddelegate LongPress:gestureRecognizer.view row:row column:column];
-
- // [griddelegate LongPress:gestureRecognizer.view row:row];
- // [griddelegate LongPress:gestureRecognizer.view _id:[self.pageData get_id:row-1]];//first row is header;
-
-
-
- }
- }
- -(void)LabelTap:(UILongPressGestureRecognizer*)gestureRecognizer {
-
- DebugLog(@"LabelTap tag = %ld",(long)gestureRecognizer.view.tag);
- // if(gestureRecognizer.view.tag - self.basetag < MAX_COLUMN) //header
- // return;
-
- // [UIView beginAnimations:nil context:nil];
- // [UIView setAnimationDuration:0.65];
- long row = gestureRecognizer.view.tag/self.column_count;
- long column = gestureRecognizer.view.tag%self.column_count;
- [UIView animateWithDuration:0.65 animations:^{
- if(self.fullrowselect)
- {
-
- for(int i=0;i<self.column_count;i++)
- {
- [self viewWithTag:(row*self.column_count+i)].alpha = 0.5;
- }
- for(int i=0;i<self.column_count;i++)
- {
- [self viewWithTag:(row*self.column_count+i)].alpha = 1.0;
- }
-
- }
- else
- {
- gestureRecognizer.view.alpha=0.5;
- gestureRecognizer.view.alpha=1;
-
- }
- }];
-
- // [UIView commitAnimations];
-
-
- if(self.fullrowselect)
- {
- if (self.griddelegate && [self.griddelegate respondsToSelector:@selector(Tap:row:)])
- [griddelegate Tap:gestureRecognizer.view row:row];
- }
- else
- if (self.griddelegate && [self.griddelegate respondsToSelector:@selector(Tap:row:column:)])
- [griddelegate Tap:gestureRecognizer.view row:row column:column];
-
- // TouchLabel* label = (TouchLabel*)gestureRecognizer.view;
- //
- //
- //
- //
- //
- // NSString * url =[self.hrefs valueForKey:label.text];
- // if(url!=nil)
- // {
- // label.textColor = [UIColor purpleColor];
- // [griddelegate Tap:label url:url];//first row is header;
- // }
-
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- }
- */
- @end
|