PageViewController.m 39 KB

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