PageViewController.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. //
  2. // DocumentViewController.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/16/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "PageViewController.h"
  9. #import "config.h"
  10. #import "const.h"
  11. #import "SignatureListViewController.h"
  12. #import "SignatureViewController.h"
  13. #import "PDFUtils.h"
  14. #import "ImageUtils.h"
  15. #import "TextUtils.h"
  16. #import "CheckSelectorViewController.h"
  17. #import "DatePickerViewController.h"
  18. //#import "TouchImageView.h"
  19. @interface PageViewController ()
  20. @end
  21. @implementation PageViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // self.pageIndex=1;
  25. // self.pdfPageView.pageIndex=self.pageIndex;
  26. self.pdfPageView.pageRef= self.pageRef;
  27. [self initControl];
  28. // UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
  29. // initWithTarget:self
  30. // action:@selector(handlePinch:)];
  31. //
  32. // [self.view addGestureRecognizer:pinchGestureRecognizer];
  33. // Do any additional setup after loading the view.
  34. }
  35. -(void) dealloc
  36. {
  37. // CFBridgingRelease(self.pageRef);
  38. }
  39. - (void)didReceiveMemoryWarning {
  40. [super didReceiveMemoryWarning];
  41. // Dispose of any resources that can be recreated.
  42. }
  43. //- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer
  44. //{
  45. //// recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
  46. //// recognizer.scale = 1;
  47. //
  48. // CGSize contentsize =self.pdfScrollView.contentSize;
  49. //
  50. // self.pdfScrollView.contentSize = CGSizeMake(contentsize.width*recognizer.scale, contentsize.height*recognizer.scale);
  51. //}
  52. -(void) initControl
  53. {
  54. int count = [self.controlTemplate[@"count"] intValue];
  55. for(int i=0;i<count;i++)
  56. {
  57. NSMutableDictionary * control =self.controlTemplate [[NSString stringWithFormat:@"control_%d",i] ];
  58. if([control[@"type"] isEqualToString:@"TextView"])
  59. {
  60. [self addTextView:control destView:self.editControlView index:i];
  61. }
  62. else if([control[@"type"] isEqualToString:@"Button"])
  63. {
  64. [self addButton:control destView:self.editControlView index:i];
  65. }
  66. else if([control[@"type"] isEqualToString:@"Check"])
  67. {
  68. [self addCheck:control destView:self.editControlView index:i];
  69. }
  70. else if([control[@"type"] isEqualToString:@"Signature"])
  71. {
  72. [self addSignatureButton:control destView:self.editControlView index:i];
  73. }
  74. else if([control[@"type"] isEqualToString:@"Image"])
  75. {
  76. [self addImage:control destView:self.editControlView index:i];
  77. }
  78. else if([control[@"type"] isEqualToString:@"Label"])
  79. {
  80. [self addLabel:control destView:self.editControlView index:i];
  81. }
  82. else if([control[@"type"] isEqualToString:@"DatePicker"])
  83. {
  84. [self addDatePicker:control destView:self.editControlView index:i];
  85. }
  86. }
  87. }
  88. #pragma mark add controls
  89. -(bool) checkActionCondition:(NSString*) condition
  90. {
  91. if([condition isEqualToString:@"true"])
  92. return true;
  93. if([condition isEqualToString:@"control_18:[value]==0"])
  94. {
  95. NSArray* arr= self.controlTemplate[@"control_18"][@"value"];
  96. if(arr.count==0)
  97. return false;
  98. else
  99. return [arr[0] intValue]==0;
  100. }
  101. return false;
  102. }
  103. -(UIView*) findControl:(NSString*)controlName
  104. {
  105. controlName=[controlName stringByReplacingOccurrencesOfString:@"control_" withString:@""];
  106. int d_idx = [controlName intValue];
  107. return [self.view viewWithTag:d_idx+CONTROL_BASE];
  108. }
  109. -(void) addTextView:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  110. {
  111. UITextView* tv = [[UITextView alloc] initWithFrame:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue])];
  112. float fontsize = [template[@"size"] floatValue];
  113. if(fontsize==0)
  114. fontsize=10;
  115. [tv setFont:[UIFont systemFontOfSize:fontsize]];
  116. bool isdisable = [template[@"disable"] boolValue];
  117. tv.editable = !isdisable;
  118. tv.text =template[@"value"];
  119. tv.tag = index+ CONTROL_BASE;
  120. //UIColorFromARGB(0x4066ccff);
  121. tv.backgroundColor = UIColorFromRGB(TV_BG);//[UIColor lightGrayColor];
  122. // tv.place
  123. // [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  124. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  125. tv.delegate = self;
  126. // [btn addTarget:self action:@selector(ControlButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  127. if(DEBUG_PDFSIG)
  128. {
  129. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  130. }
  131. [destView addSubview:tv];
  132. }
  133. -(void) addLabel:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  134. {
  135. UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue])];
  136. float fontsize = [template[@"size"] floatValue];
  137. if(fontsize==0)
  138. fontsize=10;
  139. [label setFont:[UIFont systemFontOfSize:fontsize]];
  140. label.text =template[@"value"];
  141. label.tag = index+ CONTROL_BASE;
  142. //UIColorFromARGB(0x4066ccff);
  143. // tv.backgroundColor = UIColorFromRGB(TV_BG);//[UIColor lightGrayColor];
  144. // tv.place
  145. // [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  146. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  147. // tv.delegate = self;
  148. // [btn addTarget:self action:@selector(ControlButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  149. if(DEBUG_PDFSIG)
  150. {
  151. [label setBackgroundColor:[UIColor grayColor]];
  152. }
  153. [destView addSubview:label];
  154. }
  155. -(void) addButton:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  156. {
  157. UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue])];
  158. [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  159. btn.tag = index+ CONTROL_BASE;
  160. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  161. btn.backgroundColor = UIColorFromRGB(BT_BG);
  162. [btn addTarget:self action:@selector(ControlButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  163. if(DEBUG_PDFSIG)
  164. {
  165. // btn.backgroundColor = [UIColor lightGrayColor];
  166. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  167. }
  168. [destView addSubview:btn];
  169. }
  170. -(void) addDatePicker:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  171. {
  172. UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue])];
  173. [btn setTitle:template[@"value"] forState:UIControlStateNormal];
  174. float fontsize = [template[@"size"] floatValue];
  175. if(fontsize==0)
  176. fontsize=10;
  177. btn.titleLabel.font = [UIFont systemFontOfSize: fontsize];
  178. btn.tag = index+ CONTROL_BASE;
  179. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  180. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  181. btn.backgroundColor = UIColorFromRGB(BT_BG);
  182. [btn addTarget:self action:@selector(ControlDatePickerClicked:) forControlEvents:UIControlEventTouchUpInside];
  183. if(DEBUG_PDFSIG)
  184. {
  185. // btn.backgroundColor = [UIColor lightGrayColor];
  186. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  187. }
  188. [destView addSubview:btn];
  189. }
  190. -(UIView*) createMarker:(int)size x:(int)x y:(int)y
  191. {
  192. UIView* v=[[UIView alloc] initWithFrame:CGRectMake(x, y, size, size)];
  193. return v;
  194. }
  195. -(void) addCheck:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  196. {
  197. UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue])];
  198. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  199. btn.tag = index+ CONTROL_BASE;
  200. [btn addTarget:self action:@selector(CheckButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  201. UIColor* marker_bg=UIColorFromRGB(CK_BG);//[UIColor clearColor];
  202. if(DEBUG_PDFSIG)
  203. {
  204. btn.backgroundColor = [UIColor lightGrayColor];
  205. [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  206. // marker_bg = [UIColor redColor];
  207. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  208. }
  209. [destView addSubview:btn];
  210. NSArray* cadedate = template[@"cadedate"];
  211. NSArray* checkedData = self.controlTemplate [[NSString stringWithFormat:@"control_%d",index] ][@"value"];
  212. float fontsize = [template[@"size"] floatValue];
  213. if(fontsize==0)
  214. fontsize=10;
  215. // if(checkedData.count>0)
  216. // marker_bg = [UIColor clearColor];
  217. // else
  218. marker_bg=UIColorFromRGB(CK_BG);
  219. for(int i=0;i<cadedate.count;i++)
  220. {
  221. NSArray* item = cadedate[i];
  222. NSArray* checkmarker = item[1];
  223. if(checkmarker.count==0)
  224. {
  225. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  226. btn.titleLabel.font = [UIFont systemFontOfSize: fontsize];
  227. btn.backgroundColor = UIColorFromRGB(CK_BG);
  228. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  229. {
  230. [btn setTitle:item[0][0] forState:UIControlStateNormal];
  231. }
  232. continue;
  233. }
  234. else
  235. {
  236. [btn setTitle:@"" forState:UIControlStateNormal];
  237. }
  238. UIView* marker=[self createMarker:[template[@"marker_size"] intValue] x:[item[1][0] intValue] y:[item[1][1] intValue]];
  239. marker.backgroundColor = marker_bg;
  240. marker.tag = CHECK_BASE+index*1000+i;
  241. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  242. {
  243. marker.backgroundColor= UIColorFromRGB(CK_MK);
  244. }
  245. else
  246. {
  247. marker.backgroundColor= marker_bg;
  248. }
  249. [destView addSubview:marker];
  250. }
  251. }
  252. -(void) addSignatureButton:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  253. {
  254. TouchImageView* btn = [[TouchImageView alloc] initWithFrame:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue])];
  255. btn.contentMode = UIViewContentModeScaleAspectFit;
  256. btn.delegate = self;
  257. btn.layer.borderColor = [UIColor clearColor].CGColor;
  258. btn.layer.borderWidth = 0;
  259. btn.tag = index+ CONTROL_BASE;
  260. NSString* file =template[@"value"];
  261. NSData* img_data=[ImageUtils load_img:file];
  262. if(img_data!=nil)
  263. {
  264. UIImage* image=[UIImage imageWithData:img_data];
  265. btn.image = image;
  266. }
  267. btn.backgroundColor = UIColorFromRGB(SIG_BG);
  268. if(DEBUG_PDFSIG)
  269. {
  270. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  271. }
  272. [destView addSubview:btn];
  273. }
  274. -(void) addImage:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  275. {
  276. UIImageView* btn = [[UIImageView alloc] initWithFrame:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue])];
  277. btn.contentMode = UIViewContentModeScaleAspectFit;
  278. btn.layer.borderColor = [UIColor clearColor].CGColor;
  279. btn.layer.borderWidth = 0;
  280. btn.tag = index+ CONTROL_BASE;
  281. NSString* file =template[@"value"];
  282. NSData* img_data=[ImageUtils load_img:file];
  283. if(img_data!=nil)
  284. {
  285. UIImage* image=[UIImage imageWithData:img_data];
  286. btn.image = image;
  287. }
  288. btn.backgroundColor = UIColorFromRGB(SIG_BG);
  289. if(DEBUG_PDFSIG)
  290. {
  291. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  292. }
  293. [destView addSubview:btn];
  294. }
  295. #pragma mark UIScrollView delegate
  296. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  297. UIView *subView = [scrollView viewWithTag:1024];
  298. return subView;
  299. }
  300. //- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
  301. //
  302. //
  303. //
  304. //}
  305. #pragma mark Button Click
  306. - (void)ControlDatePickerClicked:(UIButton *)sender {
  307. long index = sender.tag - CONTROL_BASE;
  308. __weak __typeof(self)weakSelf = self;
  309. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  310. [formatter setDateFormat:@"MM/dd/yyyy"];
  311. NSString* date = sender.currentTitle;
  312. NSDate* currentDate =nil;
  313. if(date.length==0)
  314. currentDate=[NSDate date];
  315. else
  316. currentDate=[formatter dateFromString:date];
  317. DatePickerViewController* dpvc =[ [UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"DatePickerViewController"];
  318. dpvc.pickerMode = UIDatePickerModeDate;
  319. dpvc.date = currentDate;
  320. dpvc.formatter = formatter;
  321. dpvc.labelTime.text = [formatter stringFromDate:currentDate];
  322. dpvc.blk_Set = ^(NSString* strdate)
  323. {
  324. // weakSelf.labelSignDate.text = strdate;
  325. [sender setTitle:strdate forState:UIControlStateNormal];
  326. weakSelf.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = strdate;
  327. };
  328. dpvc.title=@"Please choose a date";
  329. [self.navigationController pushViewController:dpvc animated:true];
  330. }
  331. - (void)ControlButtonClicked:(UIButton *)sender {
  332. // DebugLog(@"cart sort button clicked");
  333. NSLog(@"button clicked;");
  334. // [self.view addSubview:self.sortItemController.view];
  335. }
  336. - (void)CheckButtonClicked:(UIButton *)sender {
  337. // DebugLog(@"cart sort button clicked");
  338. NSLog(@"check clicked;");
  339. long index = sender.tag - CONTROL_BASE;
  340. bool single_select = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"single_select"] boolValue];
  341. bool show_detail = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"show_detail"] boolValue];
  342. NSArray* rowData = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"cadedate"];
  343. NSArray* checkedData = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"];
  344. CheckSelectorViewController *checkVC = [ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"CheckSelectorViewController"];
  345. checkVC.blk_OK = ^(NSArray* checkedData){
  346. for(int i=0;i<rowData.count;i++)
  347. {
  348. long tag=CHECK_BASE+index*1000+i;
  349. NSArray* item = rowData[i];
  350. NSArray* checkmarker = item[1];
  351. if(checkmarker.count==0)
  352. {
  353. sender.backgroundColor = UIColorFromRGB(CK_BG);
  354. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  355. {
  356. [sender setTitle:item[0][0] forState:UIControlStateNormal];
  357. if(item.count>=3)
  358. {
  359. NSMutableDictionary* action = item[2];
  360. NSArray* disable_arr = action[@"disable"] ;
  361. for(int d = 0 ; d<disable_arr.count;d++)
  362. {
  363. self.controlTemplate [disable_arr[d] ] [@"disable"] = [NSNumber numberWithBool:true];
  364. NSString* s_idx =disable_arr[d];
  365. s_idx=[s_idx stringByReplacingOccurrencesOfString:@"control_" withString:@""];
  366. int d_idx = [s_idx intValue];
  367. ((UITextView*)[sender.superview viewWithTag:d_idx+CONTROL_BASE]).editable = false;
  368. }
  369. NSArray* enable_arr = action[@"enable"] ;
  370. for(int e = 0 ; e<enable_arr.count;e++)
  371. {
  372. self.controlTemplate [enable_arr[e] ] [@"disable"] = [NSNumber numberWithBool:false];
  373. NSString* s_idx =enable_arr[e];
  374. s_idx=[s_idx stringByReplacingOccurrencesOfString:@"control_" withString:@""];
  375. int d_idx = [s_idx intValue];
  376. ((UITextView*)[sender.superview viewWithTag:d_idx+CONTROL_BASE]).editable = true;
  377. }
  378. NSMutableDictionary* json_setval=action[@"set_val"] ;
  379. for(int i=0;i<[json_setval[@"count"] intValue];i++)
  380. {
  381. NSMutableDictionary * control = json_setval [[NSString stringWithFormat:@"item_%d",i] ];
  382. UITextView* tv = (UITextView*) [self findControl:control[@"control"]];
  383. NSString* value = control[@"value"];
  384. UITextView* tv6 = (UITextView*) [self findControl:@"control_6"];
  385. NSString* text = tv6.text;
  386. NSString* newvalue = nil;
  387. if([value isEqualToString:@"*0.1"])
  388. {
  389. float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.1;
  390. newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  391. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  392. }
  393. else if([value isEqualToString:@"*0.9"])
  394. {
  395. float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.9;
  396. newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  397. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  398. }
  399. tv.text = newvalue;
  400. }
  401. }
  402. }
  403. continue;
  404. }
  405. else
  406. {
  407. [sender setTitle:@"" forState:UIControlStateNormal];
  408. }
  409. // UIView* v= sender;
  410. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  411. {
  412. // [checkedData removeObject:[NSNumber numberWithLong:indexPath.row]];
  413. [sender.superview viewWithTag:tag].backgroundColor= [UIColor blackColor];
  414. }
  415. else
  416. {
  417. // [self.checkedData addObject:[NSNumber numberWithLong:indexPath.row] ];
  418. [sender.superview viewWithTag:tag].backgroundColor= [UIColor clearColor];
  419. }
  420. }
  421. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"]=checkedData;
  422. };
  423. checkVC.rowData = rowData;
  424. checkVC.checkedData = [checkedData mutableCopy];
  425. checkVC.single_select = single_select;
  426. checkVC.show_detail = show_detail;
  427. checkVC.title=@"abcdefg";
  428. checkVC.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
  429. [self presentViewController:checkVC animated:YES completion:nil];
  430. }
  431. #pragma mark TextViewDelegate
  432. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  433. {
  434. long index = textView.tag - CONTROL_BASE;
  435. if([text isEqualToString:@""])
  436. return true;
  437. NSString* tv_format=self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"format"];
  438. if([tv_format isEqualToString:@"us_tel"])
  439. {
  440. NSMutableString *newString = [[textView.text stringByReplacingCharactersInRange:range withString:text] mutableCopy];
  441. NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
  442. NSString *decimalString = [components componentsJoinedByString:@""];
  443. NSUInteger length = decimalString.length;
  444. BOOL hasLeadingOne = length > 0 && [decimalString characterAtIndex:0] == '1';
  445. if (length == 0 ) {
  446. textView.text = decimalString;
  447. return NO;
  448. }
  449. if((length >= 10 && !hasLeadingOne) )
  450. {
  451. if(length>15)
  452. return NO;
  453. // newString=[[newString stringByReplacingOccurrencesOfString:@"-" withString:@""] mutableCopy];
  454. // [newString insertString:@"-" atIndex:14];
  455. // textView.text = newString;
  456. // return NO;
  457. }
  458. if((length >= 11))
  459. {
  460. if(length>16)
  461. return NO;
  462. // newString=[[newString stringByReplacingOccurrencesOfString:@"-" withString:@""] mutableCopy];
  463. // [newString insertString:@"-" atIndex:16];
  464. // textView.text = newString;
  465. // return NO;
  466. }
  467. NSUInteger index = 0;
  468. NSMutableString *formattedString = [NSMutableString string];
  469. if (hasLeadingOne) {
  470. [formattedString appendString:@"1 "];
  471. index += 1;
  472. }
  473. if (length - index > 3) {
  474. NSString *areaCode = [decimalString substringWithRange:NSMakeRange(index, 3)];
  475. [formattedString appendFormat:@"(%@) ",areaCode];
  476. index += 3;
  477. }
  478. if (length - index > 3) {
  479. NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 3)];
  480. [formattedString appendFormat:@"%@ ",prefix];
  481. index += 3;
  482. }
  483. if (length - index > 4) {
  484. NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 4)];
  485. [formattedString appendFormat:@"%@-",prefix];
  486. index += 4;
  487. }
  488. NSString *remainder = [decimalString substringFromIndex:index];
  489. [formattedString appendString:remainder];
  490. textView.text = formattedString;
  491. return NO;
  492. }
  493. else
  494. {
  495. NSString* oldtext =textView.text;
  496. if(oldtext==nil)
  497. oldtext=@"";
  498. bool canChange=true;
  499. int max_line = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"lines"] intValue];
  500. if(max_line==0)//默认只能有一行
  501. max_line=1;
  502. long linecount=[TextUtils countOccurencesOfString:oldtext find:@"\n"];
  503. if(max_line!=-1) //maxline -1表示不限制行数。
  504. {
  505. if(linecount<=max_line-1&& [text isEqualToString:@"\n"])
  506. {
  507. canChange = false;
  508. }
  509. }
  510. int lenth = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"length"] intValue];
  511. if(lenth==0)
  512. return canChange;
  513. else
  514. {
  515. return canChange&& (lenth>textView.text.length ||[text isEqualToString:@""]);
  516. }
  517. }
  518. }
  519. - (void)textViewDidEndEditing:(UITextView *)textView
  520. {
  521. long index = textView.tag - CONTROL_BASE;
  522. //处理 prefix 和 浮点格式化
  523. NSString* prefix_str= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  524. if(prefix_str.length==0)
  525. prefix_str = @"";
  526. NSString* text = [textView.text stringByReplacingOccurrencesOfString:prefix_str withString:@""];
  527. NSString* value_type= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value_type"];
  528. if([value_type isEqualToString:@"float"])
  529. text=[NSString stringWithFormat:@"%.2f",[text doubleValue] ];
  530. text =[NSString stringWithFormat:@"%@%@",prefix_str,text] ;
  531. textView.text = text;
  532. if(text==nil)
  533. text=@"";
  534. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = text;
  535. NSDictionary* action = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"action"];
  536. NSArray* keys=[action allKeys];
  537. for(int k=0;k<keys.count;k++)
  538. {
  539. NSMutableDictionary* act_k = action[keys[k] ];
  540. if (![self checkActionCondition:act_k[@"condition"]])
  541. continue;
  542. for(int i=0;i<[act_k[@"count"] intValue];i++)
  543. {
  544. NSMutableDictionary * control = act_k [[NSString stringWithFormat:@"item_%d",i] ];
  545. UITextView* tv = (UITextView*) [self findControl:control[@"control"]];
  546. NSString* value = control[@"value"];
  547. NSString* newvalue = nil;
  548. if([value isEqualToString:@"*0.1"])
  549. {
  550. double f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] doubleValue]*0.1;
  551. newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  552. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  553. }
  554. else if([value isEqualToString:@"*0.9"])
  555. {
  556. double f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] doubleValue]*0.9;
  557. newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  558. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  559. }
  560. else if([value isEqualToString:@"control_6-control_7"])
  561. {
  562. UITextView* tv6 = (UITextView*) [self findControl:@"control_6"];
  563. float f6= [[tv6.text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue];
  564. float f7= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue];
  565. newvalue = [NSString stringWithFormat:@"$ %.2f",f6-f7];
  566. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  567. }
  568. tv.text = newvalue;
  569. }
  570. }
  571. // int count = [self.controlTemplate[@"count"] intValue];
  572. // for(int i=0;i<count;i++)
  573. // {
  574. // NSMutableDictionary * control =;
  575. //[self update_newprice];
  576. }
  577. /*
  578. #pragma mark - Navigation
  579. // In a storyboard-based application, you will often want to do a little preparation before navigation
  580. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  581. // Get the new view controller using [segue destinationViewController].
  582. // Pass the selected object to the new view controller.
  583. }
  584. */
  585. #pragma mark signature clicked
  586. //touchimageview Delegate
  587. - (void)TouchImageViewOnTouche:(TouchImageView *)touchImageView
  588. {
  589. NSLog(@"signature button clicked;");
  590. {
  591. long index = touchImageView.tag - CONTROL_BASE;
  592. NSMutableDictionary* control_json = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ];
  593. // CGRect cellrect_screen = [RAUtils relativeFrameForScreenWithView:trigger];
  594. //
  595. //
  596. // CGRect rect1=[self.view convertRect:cellrect_screen fromView:[[[UIApplication sharedApplication] delegate] window]];
  597. //
  598. // DebugLog(@"convert1 %@",NSStringFromCGRect(rect1));
  599. SignatureListViewController *signatureVC = [ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureListViewController"];
  600. signatureVC.title=@"Signatures";
  601. signatureVC.signatureData = self.signatureData;
  602. signatureVC.subType = control_json[@"name"];
  603. // signatureVC.imageView = touchImageView;
  604. signatureVC.blk_Select =^(NSString* file)
  605. {
  606. // NSString* file_name=[file lastPathComponent];
  607. NSData* img_data=[ImageUtils load_img:file];
  608. if(img_data!=nil)
  609. {
  610. UIImage* image=[UIImage imageWithData:img_data];
  611. touchImageView.image = image;
  612. long index = touchImageView.tag - CONTROL_BASE;
  613. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  614. }
  615. };
  616. signatureVC.blk_Fill =^(NSString* file)
  617. {
  618. // // NSString* file_name=[file lastPathComponent];
  619. //
  620. // NSData* img_data=[ImageUtils load_img:file];
  621. //
  622. // if(img_data!=nil)
  623. // {
  624. //
  625. // UIImage* image=[UIImage imageWithData:img_data];
  626. // touchImageView.image = image;
  627. //
  628. // long index = touchImageView.tag - CONTROL_BASE;
  629. // self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  630. //
  631. // }
  632. };
  633. signatureVC.blk_Add = ^()
  634. {
  635. __block UIImage* signimg=nil;
  636. SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"];
  637. vc.onReturnImg = ^(UIImage* img)
  638. {
  639. signimg = img;
  640. if(signimg!=nil)
  641. {
  642. NSString* file=[PDFUtils addSignature:signimg to:self.signatureData subType:control_json[@"name"]];
  643. long index = touchImageView.tag - CONTROL_BASE;
  644. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  645. touchImageView.image = signimg;
  646. }
  647. };
  648. // orderinfoVC.url_type = URL_REMOTE;
  649. // orderinfoVC.request_url=URL_CARTDELIVERY;
  650. //
  651. // orderinfoVC.params = params;
  652. //
  653. // orderinfoVC.delegate=self;
  654. //
  655. // if(checked.count==count)
  656. // {
  657. // orderinfoVC.have_tail = true
  658. // }
  659. [self.navigationController pushViewController:vc animated:true];
  660. };
  661. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:signatureVC];
  662. navi.modalPresentationStyle=UIModalPresentationPopover;
  663. UIPopoverPresentationController* popPc = navi.popoverPresentationController;
  664. popPc.permittedArrowDirections = UIPopoverArrowDirectionAny;
  665. popPc.sourceView = touchImageView;
  666. popPc.delegate = nil;
  667. [self presentViewController:navi animated:true completion:nil];
  668. //// menu.selector = self.selector;
  669. ////
  670. //// menu.selectordelegate = self;
  671. //
  672. // // 1.创建一个UIPopover
  673. // UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:[[UINavigationController alloc] initWithRootViewController:menu]];
  674. //
  675. //
  676. //
  677. // UIPopoverPresentationController
  678. // // 2.设置尺寸
  679. // // popover.popoverContentSize = CGSizeMake(320, 44 * 5);
  680. //
  681. // // 3.从哪里显示出来 --> 指向item
  682. //
  683. // [popover presentPopoverFromRect:self.view.frame inView:self.view permittedArrowDirections:0 animated:YES];
  684. // // [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  685. //
  686. //// self.popover = popover;
  687. // popover pop
  688. //
  689. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  690. }
  691. // __block int tag = touchImageView.tag;
  692. // UIViewController* vc=[RAUtils getViewController :touchImageView];
  693. //
  694. // if(self.editable==true)
  695. // {
  696. //
  697. //
  698. // ImageUploadViewController * uploadvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"ImageUploadViewController"];
  699. //
  700. // // UIImage* img =[self.buttonImg backgroundImageForState:UIControlStateNormal];;
  701. //
  702. // if(self.img_validate)
  703. // uploadvc.img= touchImageView.image;
  704. //
  705. // uploadvc.returnValue = ^(NSString* url_down,NSString* url_up,UIImage* img)
  706. // {
  707. //
  708. // self.imgs[tag] = url_up;
  709. //
  710. // NSString* newurl=[RAUtils arr2string:self.imgs separator:@"," trim:false];
  711. //
  712. // touchImageView.image=img;
  713. //
  714. // if(self.imgChanged)
  715. // self.imgChanged(url_down,newurl,tag,url_up);
  716. //
  717. // };
  718. //
  719. // [vc.navigationController pushViewController:uploadvc animated:false];
  720. // }
  721. // else
  722. // {
  723. // if(touchImageView.image==nil)
  724. // return ;
  725. // ImageViewController * imagevc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"ImageViewController"];
  726. //
  727. //
  728. // UIImage* img=touchImageView.image;
  729. //
  730. // if(self.img_validate)
  731. // imagevc.image = img;//.imageView.image = [self.buttonImg backgroundImageForState:UIControlStateNormal];
  732. //
  733. // // uploadvc.returnValue = ^(NSString* url_down,NSString* url_up,UIImage* img)
  734. // // {
  735. // //
  736. // // [self.buttonImg setBackgroundImage:img forState:UIControlStateNormal];
  737. // //
  738. // // if(self.imgChanged)
  739. // // self.imgChanged(url_down,url_up);
  740. // //
  741. // // };
  742. //
  743. // [vc.navigationController pushViewController:imagevc animated:false];
  744. // }
  745. // // bundleVC.content_data = self.bundle_item;
  746. //
  747. }
  748. @end