PageViewController.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 = @"";
  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. NSString* oldtext =textView.text;
  436. if(oldtext==nil)
  437. oldtext=@"";
  438. bool canChange=true;
  439. int max_line = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"lines"] intValue];
  440. if(max_line==0)//默认只能有一行
  441. max_line=1;
  442. long linecount=[TextUtils countOccurencesOfString:oldtext find:@"\n"];
  443. if(max_line!=-1) //maxline -1表示不限制行数。
  444. {
  445. if(linecount<=max_line-1&& [text isEqualToString:@"\n"])
  446. {
  447. canChange = false;
  448. }
  449. }
  450. int lenth = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"length"] intValue];
  451. if(lenth==0)
  452. return canChange;
  453. else
  454. {
  455. return canChange&& (lenth>textView.text.length ||[text isEqualToString:@""]);
  456. }
  457. }
  458. - (void)textViewDidEndEditing:(UITextView *)textView
  459. {
  460. long index = textView.tag - CONTROL_BASE;
  461. //处理 prefix 和 浮点格式化
  462. NSString* prefix_str= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  463. if(prefix_str.length==0)
  464. prefix_str = @"";
  465. NSString* text = [textView.text stringByReplacingOccurrencesOfString:prefix_str withString:@""];
  466. NSString* value_type= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value_type"];
  467. if([value_type isEqualToString:@"float"])
  468. text=[NSString stringWithFormat:@"%.2f",[text floatValue] ];
  469. text =[NSString stringWithFormat:@"%@%@",prefix_str,text] ;
  470. textView.text = text;
  471. if(text==nil)
  472. text=@"";
  473. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = text;
  474. NSDictionary* action = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"action"];
  475. NSArray* keys=[action allKeys];
  476. for(int k=0;k<keys.count;k++)
  477. {
  478. NSMutableDictionary* act_k = action[keys[k] ];
  479. if (![self checkActionCondition:act_k[@"condition"]])
  480. continue;
  481. for(int i=0;i<[act_k[@"count"] intValue];i++)
  482. {
  483. NSMutableDictionary * control = act_k [[NSString stringWithFormat:@"item_%d",i] ];
  484. UITextView* tv = (UITextView*) [self findControl:control[@"control"]];
  485. NSString* value = control[@"value"];
  486. NSString* newvalue = nil;
  487. if([value isEqualToString:@"*0.1"])
  488. {
  489. float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.1;
  490. newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  491. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  492. }
  493. else if([value isEqualToString:@"*0.9"])
  494. {
  495. float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.9;
  496. newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  497. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  498. }
  499. else if([value isEqualToString:@"control_6-control_7"])
  500. {
  501. UITextView* tv6 = (UITextView*) [self findControl:@"control_6"];
  502. float f6= [[tv6.text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue];
  503. float f7= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue];
  504. newvalue = [NSString stringWithFormat:@"$ %.2f",f6-f7];
  505. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  506. }
  507. tv.text = newvalue;
  508. }
  509. }
  510. // int count = [self.controlTemplate[@"count"] intValue];
  511. // for(int i=0;i<count;i++)
  512. // {
  513. // NSMutableDictionary * control =;
  514. //[self update_newprice];
  515. }
  516. /*
  517. #pragma mark - Navigation
  518. // In a storyboard-based application, you will often want to do a little preparation before navigation
  519. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  520. // Get the new view controller using [segue destinationViewController].
  521. // Pass the selected object to the new view controller.
  522. }
  523. */
  524. #pragma mark signature clicked
  525. //touchimageview Delegate
  526. - (void)TouchImageViewOnTouche:(TouchImageView *)touchImageView
  527. {
  528. NSLog(@"signature button clicked;");
  529. {
  530. long index = touchImageView.tag - CONTROL_BASE;
  531. NSMutableDictionary* control_json = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ];
  532. // CGRect cellrect_screen = [RAUtils relativeFrameForScreenWithView:trigger];
  533. //
  534. //
  535. // CGRect rect1=[self.view convertRect:cellrect_screen fromView:[[[UIApplication sharedApplication] delegate] window]];
  536. //
  537. // DebugLog(@"convert1 %@",NSStringFromCGRect(rect1));
  538. SignatureListViewController *signatureVC = [ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureListViewController"];
  539. signatureVC.title=@"Signatures";
  540. signatureVC.signatureData = self.signatureData;
  541. signatureVC.subType = control_json[@"name"];
  542. // signatureVC.imageView = touchImageView;
  543. signatureVC.blk_Select =^(NSString* file)
  544. {
  545. // NSString* file_name=[file lastPathComponent];
  546. NSData* img_data=[ImageUtils load_img:file];
  547. if(img_data!=nil)
  548. {
  549. UIImage* image=[UIImage imageWithData:img_data];
  550. touchImageView.image = image;
  551. long index = touchImageView.tag - CONTROL_BASE;
  552. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  553. }
  554. };
  555. signatureVC.blk_Fill =^(NSString* file)
  556. {
  557. // // NSString* file_name=[file lastPathComponent];
  558. //
  559. // NSData* img_data=[ImageUtils load_img:file];
  560. //
  561. // if(img_data!=nil)
  562. // {
  563. //
  564. // UIImage* image=[UIImage imageWithData:img_data];
  565. // touchImageView.image = image;
  566. //
  567. // long index = touchImageView.tag - CONTROL_BASE;
  568. // self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  569. //
  570. // }
  571. };
  572. signatureVC.blk_Add = ^()
  573. {
  574. __block UIImage* signimg=nil;
  575. SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"];
  576. vc.onReturnImg = ^(UIImage* img)
  577. {
  578. signimg = img;
  579. if(signimg!=nil)
  580. {
  581. NSString* file=[PDFUtils addSignature:signimg to:self.signatureData subType:control_json[@"name"]];
  582. long index = touchImageView.tag - CONTROL_BASE;
  583. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  584. touchImageView.image = signimg;
  585. }
  586. };
  587. // orderinfoVC.url_type = URL_REMOTE;
  588. // orderinfoVC.request_url=URL_CARTDELIVERY;
  589. //
  590. // orderinfoVC.params = params;
  591. //
  592. // orderinfoVC.delegate=self;
  593. //
  594. // if(checked.count==count)
  595. // {
  596. // orderinfoVC.have_tail = true
  597. // }
  598. [self.navigationController pushViewController:vc animated:true];
  599. };
  600. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:signatureVC];
  601. navi.modalPresentationStyle=UIModalPresentationPopover;
  602. UIPopoverPresentationController* popPc = navi.popoverPresentationController;
  603. popPc.permittedArrowDirections = UIPopoverArrowDirectionAny;
  604. popPc.sourceView = touchImageView;
  605. popPc.delegate = nil;
  606. [self presentViewController:navi animated:true completion:nil];
  607. //// menu.selector = self.selector;
  608. ////
  609. //// menu.selectordelegate = self;
  610. //
  611. // // 1.创建一个UIPopover
  612. // UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:[[UINavigationController alloc] initWithRootViewController:menu]];
  613. //
  614. //
  615. //
  616. // UIPopoverPresentationController
  617. // // 2.设置尺寸
  618. // // popover.popoverContentSize = CGSizeMake(320, 44 * 5);
  619. //
  620. // // 3.从哪里显示出来 --> 指向item
  621. //
  622. // [popover presentPopoverFromRect:self.view.frame inView:self.view permittedArrowDirections:0 animated:YES];
  623. // // [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  624. //
  625. //// self.popover = popover;
  626. // popover pop
  627. //
  628. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  629. }
  630. // __block int tag = touchImageView.tag;
  631. // UIViewController* vc=[RAUtils getViewController :touchImageView];
  632. //
  633. // if(self.editable==true)
  634. // {
  635. //
  636. //
  637. // ImageUploadViewController * uploadvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"ImageUploadViewController"];
  638. //
  639. // // UIImage* img =[self.buttonImg backgroundImageForState:UIControlStateNormal];;
  640. //
  641. // if(self.img_validate)
  642. // uploadvc.img= touchImageView.image;
  643. //
  644. // uploadvc.returnValue = ^(NSString* url_down,NSString* url_up,UIImage* img)
  645. // {
  646. //
  647. // self.imgs[tag] = url_up;
  648. //
  649. // NSString* newurl=[RAUtils arr2string:self.imgs separator:@"," trim:false];
  650. //
  651. // touchImageView.image=img;
  652. //
  653. // if(self.imgChanged)
  654. // self.imgChanged(url_down,newurl,tag,url_up);
  655. //
  656. // };
  657. //
  658. // [vc.navigationController pushViewController:uploadvc animated:false];
  659. // }
  660. // else
  661. // {
  662. // if(touchImageView.image==nil)
  663. // return ;
  664. // ImageViewController * imagevc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"ImageViewController"];
  665. //
  666. //
  667. // UIImage* img=touchImageView.image;
  668. //
  669. // if(self.img_validate)
  670. // imagevc.image = img;//.imageView.image = [self.buttonImg backgroundImageForState:UIControlStateNormal];
  671. //
  672. // // uploadvc.returnValue = ^(NSString* url_down,NSString* url_up,UIImage* img)
  673. // // {
  674. // //
  675. // // [self.buttonImg setBackgroundImage:img forState:UIControlStateNormal];
  676. // //
  677. // // if(self.imgChanged)
  678. // // self.imgChanged(url_down,url_up);
  679. // //
  680. // // };
  681. //
  682. // [vc.navigationController pushViewController:imagevc animated:false];
  683. // }
  684. // // bundleVC.content_data = self.bundle_item;
  685. //
  686. }
  687. @end