1 +(NSArray*)splitCode:(NSString*)code backzoneIndex:(NSInteger*)outIndex 2 { 3 /* 4 01 5 0102|0304 6 010203040506|01 7 |01 8 */ 9 NSInteger backzoneIndex = 0; 10 11 NSArray* zoneArr = [code componentsSeparatedByString:@"|"]; 12 if (nil == zoneArr || [zoneArr count] != 2) 13 return nil; 14 15 NSMutableArray* splitArr = [NSMutableArray array]; 16 NSString* prozoneStr = zoneArr[0]; 17 for (int i = 0; i < [prozoneStr length]; i+= 2) 18 { 19 NSString* str = [prozoneStr substringWithRange:NSMakeRange(i, 2)]; 20 [splitArr addObject:str]; 21 } 22 backzoneIndex = [splitArr count]; 23 24 25 NSString* backzoneStr = zoneArr[1]; 26 for (int i = 0; i < [backzoneStr length]; i+= 2) 27 { 28 NSString* str = [backzoneStr substringWithRange:NSMakeRange(i, 2)]; 29 [splitArr addObject:str]; 30 } 31 *outIndex = backzoneIndex; 32 return splitArr; 33 } 34 35 +(NSString*)stdCode:(NSString*)code 36 { 37 if ([CommonHelper isEmpty:code]) 38 return @""; 39 //将,分隔,两位分隔,空格分隔的code转换成无分隔的code 40 NSRange r = [code rangeOfString:@","]; 41 if (r.location != NSNotFound) 42 { 43 NSString* newCode = [code stringByReplacingOccurrencesOfString:@"," withString:@""]; 44 return newCode; 45 } 46 r = [code rangeOfString:@" "]; 47 { 48 NSString* newCode = [code stringByReplacingOccurrencesOfString:@" " withString:@","]; 49 return newCode; 50 } 51 return code; 52 } 53 +(UIView*)makeLotteryView:(NSString*)code font:(UIFont*)font height:(CGFloat)height sep:(CGFloat)sep borderColor:(UIColor*)boderColor prozoneColor:(UIColor*)prozoneColor backzoneColor:(UIColor*)backzoneColor 54 { 55 NSString* stdCode = [MyLotteryShoppingCartViewController stdCode:code]; 56 if ([CommonHelper isEmpty:stdCode]) 57 return nil; 58 NSInteger backzoneIndex = 0; 59 NSArray* codeArr = [MyLotteryShoppingCartViewController splitCode:stdCode backzoneIndex:&backzoneIndex]; 60 if ([CommonHelper isEmptyArray:codeArr]) 61 return nil; 62 NSInteger codeCount = [codeArr count]; 63 CGFloat viewWidth = codeCount*height + (codeCount -1)*sep; 64 UIView* contentView = [UIView new]; 65 CGRect rect = CGRectMake(0, 0,viewWidth,height); 66 [contentView setFrame:rect]; 67 CGFloat codeX = 0; 68 CGFloat codeY = 0; 69 CGFloat codeSep = sep; 70 CGFloat codeW = height; 71 CGFloat codeH = height; 72 for(int i = 0; i< codeCount;i++) 73 { 74 NSString* codeStr = codeArr[i]; 75 rect = CGRectMake(codeX, codeY, codeW, codeH); 76 UILabel* lblCode = [[UILabel alloc]initWithFrame:rect]; 77 [lblCode setBackgroundColor:[UIColor clearColor]]; 78 [lblCode setFont:font]; 79 [lblCode.layer setCornerRadius:height/2]; 80 [lblCode.layer setMasksToBounds:YES]; 81 [lblCode.layer setBorderWidth:1]; 82 lblCode.layer.borderColor = boderColor.CGColor; 83 [lblCode setTextColor:(i >= backzoneIndex) ? backzoneColor :prozoneColor]; 84 [lblCode setText:codeStr]; 85 [lblCode setTextAlignment:NSTextAlignmentCenter]; 86 [contentView addSubview:lblCode]; 87 codeX += codeW+ codeSep; 88 } 89 return contentView; 90 }
第一个版本,先用View顶过去,后面考虑drawRect,目前看对帧率是有影响的