PageViewController.m 38 KB

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