PageViewController.m 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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. #define NUMBERS @"0123456789.\n"
  9. #import "PageViewController.h"
  10. #import "config.h"
  11. #import "const.h"
  12. #import "SignatureListViewController.h"
  13. #import "SignatureViewController.h"
  14. #import "PDFUtils.h"
  15. #import "ImageUtils.h"
  16. #import "TextUtils.h"
  17. #import "CheckSelectorViewController.h"
  18. #import "DatePickerViewController.h"
  19. //#import "TouchImageView.h"
  20. @interface PageViewController ()
  21. @end
  22. @implementation PageViewController
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. //
  26. // self.pdfScrollView = [[PDFScrollView alloc]initWithFrame:CGRectMake(0, 64, 768, 960)];
  27. // self.pdfScrollView.backgroundColor= [UIColor redColor];
  28. //self.edgesForExtendedLayout = UIRectEdgeNone;
  29. // self.pageIndex=1;
  30. // self.pdfPageView.pageIndex=self.pageIndex;
  31. self.pdfPageView.pageRef= self.pageRef;
  32. self.pdfScrollView.contentSize = self.pdfScrollView.frame.size;
  33. self.keyboard_h=0;
  34. [self initControl];
  35. // UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
  36. // initWithTarget:self
  37. // action:@selector(handlePinch:)];
  38. //
  39. // [self.view addGestureRecognizer:pinchGestureRecognizer];
  40. // Do any additional setup after loading the view.
  41. }
  42. -(void)viewWillAppear:(BOOL)animated
  43. {
  44. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  45. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  46. // 键盘高度变化通知,ios5.0新增的
  47. #ifdef __IPHONE_5_0
  48. float version = [[[UIDevice currentDevice] systemVersion] floatValue];
  49. if (version >= 5.0) {
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  51. }
  52. #endif
  53. // self.pdfScrollView.contentSize = self.pdfScrollView.frame.size;
  54. [self refreshControl];
  55. }
  56. -(void) viewWillDisappear:(BOOL)animated
  57. {
  58. [self.hotTextView endEditing:true];
  59. [[NSNotificationCenter defaultCenter] removeObserver:self];
  60. }
  61. -(void) dealloc
  62. {
  63. // CFBridgingRelease(self.pageRef);
  64. }
  65. - (void)didReceiveMemoryWarning {
  66. [super didReceiveMemoryWarning];
  67. // Dispose of any resources that can be recreated.
  68. }
  69. //- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer
  70. //{
  71. //// recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
  72. //// recognizer.scale = 1;
  73. //
  74. // CGSize contentsize =self.pdfScrollView.contentSize;
  75. //
  76. // self.pdfScrollView.contentSize = CGSizeMake(contentsize.width*recognizer.scale, contentsize.height*recognizer.scale);
  77. //}
  78. //+(CGRect)WindowRect2PDFRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size
  79. //{
  80. // CGRect pdf_expand;
  81. // float offset_x = 0;
  82. // float offset_y = 0;
  83. // float scale =1;
  84. // if(pdf_rect.size.height>=pdf_rect.size.width)
  85. // {
  86. // pdf_expand = CGRectMake(0, 0, pdf_rect.size.height*window_size.width/window_size.height, pdf_rect.size.height);
  87. // offset_x = (pdf_expand.size.width-pdf_rect.size.width)/2;
  88. // scale = pdf_rect.size.height/window_size.height;
  89. //
  90. // }
  91. // else
  92. // {
  93. // pdf_expand = CGRectMake(0, 0, pdf_rect.size.width, pdf_rect.size.width*window_size.height/window_size.width);
  94. // offset_y = (pdf_expand.size.height-pdf_rect.size.height)/2;
  95. // scale = pdf_rect.size.width/window_size.width;
  96. // }
  97. // float x=rect.origin.x*scale-offset_x;
  98. // float y=rect.origin.y*scale-offset_y;
  99. // float width = rect.size.width*scale;
  100. // float height = rect.size.height*scale;
  101. //
  102. // return CGRectMake(x, y, width, height);
  103. //
  104. //}
  105. -(CGRect) scaleControl:(CGRect) frame from:(CGSize)fwindowsize to:(CGSize)twindowsize
  106. {
  107. CGRect pdfsize=CGPDFPageGetBoxRect(self.pageRef, kCGPDFMediaBox);
  108. frame=[PDFUtils WindowRect2PDFRect:frame pdf_rect:pdfsize window_size:fwindowsize];
  109. frame= [PDFUtils PDFRect2WindowRect:frame pdf_rect:pdfsize window_size:twindowsize];
  110. return frame;
  111. }
  112. -(void) initControl
  113. {
  114. // return;
  115. int count = [self.controlTemplate[@"count"] intValue];
  116. for(int i=0;i<count;i++)
  117. {
  118. NSMutableDictionary * control =self.controlTemplate [[NSString stringWithFormat:@"control_%d",i] ];
  119. if([control[@"type"] isEqualToString:@"TextView"])
  120. {
  121. [self addTextView:control destView:self.editControlView index:i];
  122. }
  123. else if([control[@"type"] isEqualToString:@"Button"])
  124. {
  125. [self addButton:control destView:self.editControlView index:i];
  126. }
  127. else if([control[@"type"] isEqualToString:@"Check"])
  128. {
  129. [self addCheck:control destView:self.editControlView index:i];
  130. }
  131. else if([control[@"type"] isEqualToString:@"Signature"])
  132. {
  133. [self addSignatureButton:control destView:self.editControlView index:i];
  134. }
  135. else if([control[@"type"] isEqualToString:@"Image"])
  136. {
  137. [self addImage:control destView:self.editControlView index:i];
  138. }
  139. else if([control[@"type"] isEqualToString:@"Label"])
  140. {
  141. [self addLabel:control destView:self.editControlView index:i];
  142. }
  143. else if([control[@"type"] isEqualToString:@"DatePicker"])
  144. {
  145. [self addDatePicker:control destView:self.editControlView index:i];
  146. }
  147. }
  148. }
  149. -(void) refreshControl
  150. {
  151. int count = [self.controlTemplate[@"count"] intValue];
  152. for(int i=0;i<count;i++)
  153. {
  154. NSString* controlname =[NSString stringWithFormat:@"control_%d",i];
  155. NSMutableDictionary * control =self.controlTemplate [ controlname];
  156. // if([control[@"type"] isEqualToString:@"TextView"])
  157. // {
  158. // UITextView* tv = (UITextView*) [self findControl:controlname];
  159. // tv.text = control[@"value"];
  160. // }
  161. // else if([control[@"type"] isEqualToString:@"Button"])
  162. // {
  163. // [self addButton:control destView:self.editControlView index:i];
  164. //
  165. // }
  166. // else if([control[@"type"] isEqualToString:@"Check"])
  167. // {
  168. // [self addCheck:control destView:self.editControlView index:i];
  169. //
  170. // }
  171. // else
  172. if([control[@"type"] isEqualToString:@"Signature"])
  173. {
  174. TouchImageView* btn = (TouchImageView*) [self findControl:controlname];
  175. NSString* file =control[@"value"];
  176. NSData* img_data=[ImageUtils load_img:file];
  177. if(img_data!=nil)
  178. {
  179. UIImage* image=[UIImage imageWithData:img_data];
  180. btn.image = image;
  181. }
  182. }
  183. // else if([control[@"type"] isEqualToString:@"Image"])
  184. // {
  185. // [self addImage:control destView:self.editControlView index:i];
  186. //
  187. // }
  188. // else if([control[@"type"] isEqualToString:@"Label"])
  189. // {
  190. // [self addLabel:control destView:self.editControlView index:i];
  191. //
  192. // }
  193. // else if([control[@"type"] isEqualToString:@"DatePicker"])
  194. // {
  195. // [self addDatePicker:control destView:self.editControlView index:i];
  196. //
  197. // }
  198. }
  199. }
  200. -(void)EndEditing
  201. {
  202. [[self view] endEditing:YES];
  203. }
  204. #pragma mark add controls
  205. -(bool) checkActionCondition:(NSString*) condition
  206. {
  207. if([condition isEqualToString:@"true"])
  208. return true;
  209. if([condition isEqualToString:@"control_18:[value]==0"])
  210. {
  211. NSArray* arr= self.controlTemplate[@"control_18"][@"value"];
  212. if(arr.count==0)
  213. return false;
  214. else
  215. return [arr[0] intValue]==0;
  216. }
  217. return false;
  218. }
  219. -(UIView*) findControl:(NSString*)controlName
  220. {
  221. controlName=[controlName stringByReplacingOccurrencesOfString:@"control_" withString:@""];
  222. int d_idx = [controlName intValue];
  223. return [self.view viewWithTag:d_idx+CONTROL_BASE];
  224. }
  225. -(void) addTextView:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  226. {
  227. CGRect srect=[self scaleControl:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue]) from:CGSizeMake(768, 960.5) to:destView.frame.size];
  228. UITextView* tv = [[UITextView alloc] initWithFrame:srect];
  229. float fontsize = [template[@"size"] floatValue];
  230. if(fontsize==0)
  231. fontsize=10;
  232. [tv setFont:[UIFont systemFontOfSize:fontsize]];
  233. bool isdisable = [template[@"disable"] boolValue];
  234. tv.editable = !isdisable;
  235. NSString* text= template[@"value"];
  236. if(text.length==0)
  237. text=@" ";
  238. tv.text =text;
  239. float tv_vmargin= [template[@"tv_vmargin"] floatValue];
  240. NSString* capital= template[@"capital"];
  241. UITextAutocapitalizationType capitalType=UITextAutocapitalizationTypeWords;
  242. if([capital isEqualToString:@"charater"])
  243. {
  244. capitalType=UITextAutocapitalizationTypeAllCharacters;
  245. }
  246. else if([capital isEqualToString:@"sentences"])
  247. {
  248. capitalType=UITextAutocapitalizationTypeSentences;
  249. }
  250. else if([capital isEqualToString:@"none"])
  251. {
  252. capitalType=UITextAutocapitalizationTypeNone;
  253. }
  254. else
  255. {
  256. capitalType=UITextAutocapitalizationTypeWords;
  257. }
  258. tv.autocapitalizationType=capitalType;
  259. NSMutableAttributedString* attr_str= [tv.attributedText mutableCopy];
  260. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  261. if(tv_vmargin!=0)
  262. paragraphStyle.lineHeightMultiple = tv_vmargin;
  263. // paragraphStyle.maximumLineHeight = 25.f;
  264. // paragraphStyle.minimumLineHeight = 15.f;
  265. // paragraphStyle.firstLineHeadIndent = 20.f;
  266. // paragraphStyle.alignment = NSTextAlignmentJustified;
  267. [attr_str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,attr_str.length)];
  268. tv.attributedText=attr_str;
  269. if([text isEqualToString:(@" ")])
  270. tv.text=@"";
  271. tv.tag = index+ CONTROL_BASE;
  272. tv.scrollEnabled=false;
  273. NSString* tv_format=template[@"keyboard"];
  274. if([tv_format isEqualToString:@"tel"])
  275. {
  276. tv.keyboardType=UIKeyboardTypePhonePad;
  277. }
  278. else if([tv_format isEqualToString:@"number"])
  279. {
  280. tv.keyboardType=UIKeyboardTypeNumberPad;
  281. }
  282. else if([tv_format isEqualToString:@"email"])
  283. {
  284. tv.keyboardType=UIKeyboardTypeEmailAddress;
  285. }
  286. else
  287. {
  288. tv.keyboardType=UIKeyboardTypeDefault;
  289. }
  290. //UIColorFromARGB(0x4066ccff);
  291. tv.backgroundColor = UIColorFromRGB(TV_BG);//[UIColor lightGrayColor];
  292. // tv.place
  293. // [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  294. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  295. tv.delegate = self;
  296. // [btn addTarget:self action:@selector(ControlButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  297. if(DEBUG_PDFSIG)
  298. {
  299. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  300. }
  301. [destView addSubview:tv];
  302. }
  303. -(void) addLabel:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  304. {
  305. CGRect srect=[self scaleControl:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue]) from:CGSizeMake(768, 960.5) to:destView.frame.size];
  306. UILabel* label = [[UILabel alloc] initWithFrame:srect];
  307. float fontsize = [template[@"size"] floatValue];
  308. if(fontsize==0)
  309. fontsize=10;
  310. [label setFont:[UIFont systemFontOfSize:fontsize]];
  311. label.text =template[@"value"];
  312. label.tag = index+ CONTROL_BASE;
  313. //UIColorFromARGB(0x4066ccff);
  314. // tv.backgroundColor = UIColorFromRGB(TV_BG);//[UIColor lightGrayColor];
  315. // tv.place
  316. // [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  317. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  318. // tv.delegate = self;
  319. // [btn addTarget:self action:@selector(ControlButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  320. if(DEBUG_PDFSIG)
  321. {
  322. [label setBackgroundColor:[UIColor grayColor]];
  323. }
  324. [destView addSubview:label];
  325. }
  326. -(void) addButton:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  327. {
  328. CGRect srect=[self scaleControl:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue]) from:CGSizeMake(768, 960.5) to:destView.frame.size];
  329. UIButton* btn = [[UIButton alloc] initWithFrame:srect];
  330. [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  331. btn.tag = index+ CONTROL_BASE;
  332. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  333. btn.backgroundColor = UIColorFromRGB(BT_BG);
  334. [btn addTarget:self action:@selector(ControlButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  335. if(DEBUG_PDFSIG)
  336. {
  337. // btn.backgroundColor = [UIColor lightGrayColor];
  338. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  339. }
  340. [destView addSubview:btn];
  341. }
  342. -(void) addDatePicker:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  343. {
  344. CGRect srect=[self scaleControl:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue]) from:CGSizeMake(768, 960.5) to:destView.frame.size];
  345. UIButton* btn = [[UIButton alloc] initWithFrame:srect];
  346. [btn setTitle:template[@"value"] forState:UIControlStateNormal];
  347. float fontsize = [template[@"size"] floatValue];
  348. if(fontsize==0)
  349. fontsize=10;
  350. btn.titleLabel.font = [UIFont systemFontOfSize: fontsize];
  351. btn.tag = index+ CONTROL_BASE;
  352. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  353. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  354. btn.backgroundColor = UIColorFromRGB(BT_BG);
  355. [btn addTarget:self action:@selector(ControlDatePickerClicked:) forControlEvents:UIControlEventTouchUpInside];
  356. if(DEBUG_PDFSIG)
  357. {
  358. // btn.backgroundColor = [UIColor lightGrayColor];
  359. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  360. }
  361. [destView addSubview:btn];
  362. }
  363. -(UIView*) createMarker:(int)size x:(int)x y:(int)y destView:(UIView*)destView
  364. {
  365. CGRect srect=[self scaleControl:CGRectMake(x,y,size,size) from:CGSizeMake(768, 960.5) to:destView.frame.size];
  366. UIView* v=[[UIView alloc] initWithFrame:srect];
  367. return v;
  368. }
  369. -(void) addCheck:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  370. {
  371. CGRect srect=[self scaleControl:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue]) from:CGSizeMake(768, 960.5) to:destView.frame.size];
  372. UIButton* btn = [[UIButton alloc] initWithFrame:srect];
  373. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  374. btn.tag = index+ CONTROL_BASE;
  375. [btn addTarget:self action:@selector(CheckButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  376. UIColor* marker_bg=UIColorFromRGB(CK_BG);//[UIColor clearColor];
  377. if(DEBUG_PDFSIG)
  378. {
  379. btn.backgroundColor = [UIColor lightGrayColor];
  380. [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  381. // marker_bg = [UIColor redColor];
  382. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  383. }
  384. [destView addSubview:btn];
  385. NSArray* cadedate = template[@"cadedate"];
  386. NSArray* checkedData = self.controlTemplate [[NSString stringWithFormat:@"control_%d",index] ][@"value"];
  387. float fontsize = [template[@"size"] floatValue];
  388. if(fontsize==0)
  389. fontsize=10;
  390. // if(checkedData.count>0)
  391. // marker_bg = [UIColor clearColor];
  392. // else
  393. marker_bg=UIColorFromRGB(CK_BG);
  394. for(int i=0;i<cadedate.count;i++)
  395. {
  396. NSArray* item = cadedate[i];
  397. NSArray* checkmarker = item[1];
  398. if(checkmarker.count==0)
  399. {
  400. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  401. btn.titleLabel.font = [UIFont systemFontOfSize: fontsize];
  402. btn.backgroundColor = UIColorFromRGB(CK_BG);
  403. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  404. {
  405. [btn setTitle:item[0][0] forState:UIControlStateNormal];
  406. }
  407. continue;
  408. }
  409. else
  410. {
  411. [btn setTitle:@"" forState:UIControlStateNormal];
  412. }
  413. UIView* marker=[self createMarker:[template[@"marker_size"] intValue] x:[item[1][0] intValue] y:[item[1][1] intValue] destView:destView];
  414. marker.backgroundColor = marker_bg;
  415. marker.tag = CHECK_BASE+index*1000+i;
  416. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  417. {
  418. marker.backgroundColor= UIColorFromRGB(CK_MK);
  419. }
  420. else
  421. {
  422. marker.backgroundColor= marker_bg;
  423. }
  424. [destView addSubview:marker];
  425. }
  426. }
  427. -(void) addSignatureButton:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  428. {
  429. CGRect srect=[self scaleControl:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue]) from:CGSizeMake(768, 960.5) to:destView.frame.size];
  430. TouchImageView* btn = [[TouchImageView alloc] initWithFrame:srect];
  431. btn.contentMode = UIViewContentModeScaleAspectFit;
  432. btn.delegate = self;
  433. btn.layer.borderColor = [UIColor clearColor].CGColor;
  434. btn.layer.borderWidth = 0;
  435. btn.tag = index+ CONTROL_BASE;
  436. NSString* file =template[@"value"];
  437. NSData* img_data=[ImageUtils load_img:file];
  438. if(img_data!=nil)
  439. {
  440. UIImage* image=[UIImage imageWithData:img_data];
  441. btn.image = image;
  442. }
  443. btn.backgroundColor = UIColorFromRGB(SIG_BG);
  444. if(DEBUG_PDFSIG)
  445. {
  446. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  447. }
  448. [destView addSubview:btn];
  449. }
  450. -(void) addImage:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  451. {
  452. CGRect srect=[self scaleControl:CGRectMake([template[@"pos_x"] floatValue], [template[@"pos_y"] floatValue], [template[@"width"] floatValue], [template[@"height"] floatValue]) from:CGSizeMake(768, 960.5) to:destView.frame.size];
  453. UIImageView* btn = [[UIImageView alloc] initWithFrame:srect];
  454. btn.contentMode = UIViewContentModeScaleAspectFit;
  455. btn.layer.borderColor = [UIColor clearColor].CGColor;
  456. btn.layer.borderWidth = 0;
  457. btn.tag = index+ CONTROL_BASE;
  458. NSString* file =template[@"value"];
  459. NSData* img_data=[ImageUtils load_img:file];
  460. if(img_data!=nil)
  461. {
  462. UIImage* image=[UIImage imageWithData:img_data];
  463. btn.image = image;
  464. }
  465. btn.backgroundColor = UIColorFromRGB(SIG_BG);
  466. if(DEBUG_PDFSIG)
  467. {
  468. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  469. }
  470. [destView addSubview:btn];
  471. }
  472. #pragma mark UIScrollView delegate
  473. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  474. UIView *subView = [scrollView viewWithTag:1024];
  475. return subView;
  476. }
  477. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale
  478. {
  479. CGPoint contentOffsetPoint = self.pdfScrollView.contentOffset;
  480. CGSize contentSize =self.pdfScrollView.contentSize;//frame.size;
  481. // CGSize size1 = self.pdfScrollView.bounds.size;
  482. // NSLog(@"frame:%@ bound:%@", NSStringFromCGSize(contentSize),NSStringFromCGSize(size1) );
  483. contentSize.height+=self.keyboard_h;
  484. contentOffsetPoint.y+=self.keyboard_h;
  485. self.pdfScrollView.contentSize =contentSize;
  486. self.pdfScrollView.contentOffset=contentOffsetPoint;
  487. }
  488. //- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
  489. //
  490. //
  491. //
  492. //}
  493. #pragma mark Button Click
  494. - (void)ControlDatePickerClicked:(UIButton *)sender {
  495. long index = sender.tag - CONTROL_BASE;
  496. __weak __typeof(self)weakSelf = self;
  497. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  498. [formatter setDateFormat:@"MM/dd/yyyy"];
  499. NSString* date = sender.currentTitle;
  500. NSDate* currentDate =nil;
  501. if(date.length==0)
  502. currentDate=[NSDate date];
  503. else
  504. currentDate=[formatter dateFromString:date];
  505. DatePickerViewController* dpvc =[ [UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"DatePickerViewController"];
  506. dpvc.pickerMode = UIDatePickerModeDate;
  507. dpvc.date = currentDate;
  508. dpvc.formatter = formatter;
  509. dpvc.labelTime.text = [formatter stringFromDate:currentDate];
  510. dpvc.blk_Set = ^(NSString* strdate)
  511. {
  512. // weakSelf.labelSignDate.text = strdate;
  513. [sender setTitle:strdate forState:UIControlStateNormal];
  514. weakSelf.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = strdate;
  515. };
  516. dpvc.title=@"Please choose a date";
  517. [self.navigationController pushViewController:dpvc animated:true];
  518. }
  519. - (void)ControlButtonClicked:(UIButton *)sender {
  520. // DebugLog(@"cart sort button clicked");
  521. NSLog(@"button clicked;");
  522. // [self.view addSubview:self.sortItemController.view];
  523. }
  524. - (void)CheckButtonClicked:(UIButton *)sender {
  525. // DebugLog(@"cart sort button clicked");
  526. NSLog(@"check clicked;");
  527. long index = sender.tag - CONTROL_BASE;
  528. bool single_select = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"single_select"] boolValue];
  529. bool show_detail = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"show_detail"] boolValue];
  530. NSArray* rowData = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"cadedate"];
  531. NSArray* checkedData = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"];
  532. CheckSelectorViewController *checkVC = [ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"CheckSelectorViewController"];
  533. checkVC.blk_OK = ^(NSArray* checkedData){
  534. for(int i=0;i<rowData.count;i++)
  535. {
  536. long tag=CHECK_BASE+index*1000+i;
  537. NSArray* item = rowData[i];
  538. NSArray* checkmarker = item[1];
  539. if(checkmarker.count==0)
  540. {
  541. sender.backgroundColor = UIColorFromRGB(CK_BG);
  542. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  543. {
  544. [sender setTitle:item[0][0] forState:UIControlStateNormal];
  545. if(item.count>=3)
  546. {
  547. NSMutableDictionary* action = item[2];
  548. NSArray* disable_arr = action[@"disable"] ;
  549. for(int d = 0 ; d<disable_arr.count;d++)
  550. {
  551. self.controlTemplate [disable_arr[d] ] [@"disable"] = [NSNumber numberWithBool:true];
  552. NSString* s_idx =disable_arr[d];
  553. s_idx=[s_idx stringByReplacingOccurrencesOfString:@"control_" withString:@""];
  554. int d_idx = [s_idx intValue];
  555. ((UITextView*)[sender.superview viewWithTag:d_idx+CONTROL_BASE]).editable = false;
  556. }
  557. NSArray* enable_arr = action[@"enable"] ;
  558. for(int e = 0 ; e<enable_arr.count;e++)
  559. {
  560. self.controlTemplate [enable_arr[e] ] [@"disable"] = [NSNumber numberWithBool:false];
  561. NSString* s_idx =enable_arr[e];
  562. s_idx=[s_idx stringByReplacingOccurrencesOfString:@"control_" withString:@""];
  563. int d_idx = [s_idx intValue];
  564. ((UITextView*)[sender.superview viewWithTag:d_idx+CONTROL_BASE]).editable = true;
  565. }
  566. NSMutableDictionary* json_setval=action[@"set_val"] ;
  567. for(int i=0;i<[json_setval[@"count"] intValue];i++)
  568. {
  569. NSMutableDictionary * control = json_setval [[NSString stringWithFormat:@"item_%d",i] ];
  570. UITextView* tv = (UITextView*) [self findControl:control[@"control"]];
  571. NSString* value = control[@"value"];
  572. UITextView* tv6 = (UITextView*) [self findControl:@"control_6"];
  573. NSString* text = tv6.text;
  574. NSString* newvalue = nil;
  575. if([value isEqualToString:@"*0.1"])
  576. {
  577. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  578. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  579. NSString* prefix_str= self.controlTemplate [control[@"control"] ][@"prefix_str"];
  580. if(prefix_str.length==0)
  581. prefix_str = @"";
  582. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  583. double f = [[formatter numberFromString:text] doubleValue]*0.1;
  584. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  585. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  586. // float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.1;
  587. // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  588. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  589. }
  590. else if([value isEqualToString:@"*0.9"])
  591. {
  592. // float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.9;
  593. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  594. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  595. NSString* prefix_str= self.controlTemplate [control[@"control"] ][@"prefix_str"];
  596. if(prefix_str.length==0)
  597. prefix_str = @"";
  598. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  599. double f = [[formatter numberFromString:text] doubleValue]*0.9;
  600. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  601. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  602. // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  603. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  604. }
  605. tv.text = newvalue;
  606. }
  607. }
  608. }
  609. continue;
  610. }
  611. else
  612. {
  613. [sender setTitle:@"" forState:UIControlStateNormal];
  614. }
  615. // UIView* v= sender;
  616. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  617. {
  618. // [checkedData removeObject:[NSNumber numberWithLong:indexPath.row]];
  619. [sender.superview viewWithTag:tag].backgroundColor= [UIColor blackColor];
  620. }
  621. else
  622. {
  623. // [self.checkedData addObject:[NSNumber numberWithLong:indexPath.row] ];
  624. [sender.superview viewWithTag:tag].backgroundColor= [UIColor clearColor];
  625. }
  626. }
  627. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"]=checkedData;
  628. };
  629. checkVC.rowData = rowData;
  630. checkVC.checkedData = [checkedData mutableCopy];
  631. checkVC.single_select = single_select;
  632. checkVC.show_detail = show_detail;
  633. checkVC.title=@"abcdefg";
  634. checkVC.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
  635. [self presentViewController:checkVC animated:YES completion:nil];
  636. }
  637. #pragma mark TextViewDelegate
  638. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  639. {
  640. long index = textView.tag - CONTROL_BASE;
  641. if([text isEqualToString:@""])
  642. return true;
  643. if([text isEqualToString:@"\n"])
  644. {
  645. [textView resignFirstResponder];
  646. return false;
  647. }
  648. NSString* tv_format=self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"format"];
  649. if([tv_format isEqualToString:@"us_tel"])
  650. {
  651. NSMutableString *newString = [[textView.text stringByReplacingCharactersInRange:range withString:text] mutableCopy];
  652. NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
  653. NSString *decimalString = [components componentsJoinedByString:@""];
  654. NSUInteger length = decimalString.length;
  655. BOOL hasLeadingOne = length > 0 && [decimalString characterAtIndex:0] == '1';
  656. if (length == 0 ) {
  657. textView.text = decimalString;
  658. return NO;
  659. }
  660. if((length >= 10 && !hasLeadingOne) )
  661. {
  662. if(length>15)
  663. return NO;
  664. // newString=[[newString stringByReplacingOccurrencesOfString:@"-" withString:@""] mutableCopy];
  665. // [newString insertString:@"-" atIndex:14];
  666. // textView.text = newString;
  667. // return NO;
  668. }
  669. if((length >= 11))
  670. {
  671. if(length>16)
  672. return NO;
  673. // newString=[[newString stringByReplacingOccurrencesOfString:@"-" withString:@""] mutableCopy];
  674. // [newString insertString:@"-" atIndex:16];
  675. // textView.text = newString;
  676. // return NO;
  677. }
  678. NSUInteger index = 0;
  679. NSMutableString *formattedString = [NSMutableString string];
  680. if (hasLeadingOne) {
  681. [formattedString appendString:@"1 "];
  682. index += 1;
  683. }
  684. if (length - index > 3) {
  685. NSString *areaCode = [decimalString substringWithRange:NSMakeRange(index, 3)];
  686. [formattedString appendFormat:@"(%@) ",areaCode];
  687. index += 3;
  688. }
  689. if (length - index > 3) {
  690. NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 3)];
  691. [formattedString appendFormat:@"%@ ",prefix];
  692. index += 3;
  693. }
  694. if (length - index > 4) {
  695. NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 4)];
  696. [formattedString appendFormat:@"%@-",prefix];
  697. index += 4;
  698. }
  699. NSString *remainder = [decimalString substringFromIndex:index];
  700. [formattedString appendString:remainder];
  701. textView.text = formattedString;
  702. return NO;
  703. }
  704. else if([tv_format isEqualToString:@"price"])
  705. {
  706. // NSMutableString *newString = [[textView.text stringByReplacingCharactersInRange:range withString:text] mutableCopy];
  707. // NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
  708. // NSString *decimalString = [components componentsJoinedByString:@""];
  709. //
  710. //
  711. //
  712. //
  713. // NSString *newprice = [formatter stringFromNumber:[NSNumber numberWithInt: [decimalString intValue]]];
  714. //
  715. // textView.text = newprice;
  716. NSCharacterSet *cs;
  717. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  718. //
  719. NSString *filtered = [[text componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  720. BOOL canChange = [text isEqualToString:filtered];
  721. // if(canChange)
  722. // {
  723. //
  724. // }
  725. return canChange;
  726. // return NO;
  727. }
  728. else
  729. {
  730. NSString* oldtext =textView.text;
  731. if(oldtext==nil)
  732. oldtext=@"";
  733. bool canChange=true;
  734. int max_line = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"lines"] intValue];
  735. if(max_line==0)//默认只能有一行
  736. max_line=1;
  737. long linecount=[TextUtils countOccurencesOfString:oldtext find:@"\n"];
  738. if(max_line!=-1) //maxline -1表示不限制行数。
  739. {
  740. if(linecount<=max_line-1&& [text isEqualToString:@"\n"])
  741. {
  742. canChange = false;
  743. }
  744. }
  745. int lenth = [self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"length"] intValue];
  746. if(lenth==0)
  747. return canChange;
  748. else
  749. {
  750. return canChange&& (lenth>textView.text.length ||[text isEqualToString:@""]);
  751. }
  752. }
  753. }
  754. //- (void)textViewDidBeginEditing:(UITextView *)textView
  755. //{
  756. // self.hotTextView = textView;
  757. //}
  758. - (void)textViewDidBeginEditing:(UITextView *)textView
  759. {
  760. long index = textView.tag - CONTROL_BASE;
  761. NSString* tv_format=self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"format"];
  762. if([tv_format isEqualToString:@"price"])
  763. {
  764. NSString* prefix_str= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  765. if(prefix_str.length==0)
  766. prefix_str = @"";
  767. NSString* text =[textView.text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  768. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  769. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  770. NSNumber* number= [formatter numberFromString:text];
  771. if(number!=nil)
  772. textView.text = [NSString stringWithFormat:@"%.2f",[number doubleValue]];
  773. }
  774. // NSRange r = textView.selectedRange;
  775. //
  776. //
  777. // textView.selectedRange = NSMakeRange(textView.text.length, 0);
  778. // NSRange range;
  779. //
  780. // range.location = textView.text.length;
  781. //
  782. // range.length = 0;
  783. //
  784. // textView.selectedRange = range;
  785. }
  786. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
  787. {
  788. NSLog(@"textViewShouldBeginEditing");
  789. self.hotTextView = textView;//(UITableViewCell*)textView.superview.superview;
  790. return textView.editable;
  791. }
  792. - (void)textViewDidEndEditing:(UITextView *)textView
  793. {
  794. long index = textView.tag - CONTROL_BASE;
  795. //处理 prefix 和 浮点格式化
  796. NSString* prefix_str= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  797. if(prefix_str.length==0)
  798. prefix_str = @"";
  799. NSString* text =textView.text;// [textView.text stringByReplacingOccurrencesOfString:prefix_str withString:@""];
  800. NSString* value_type= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value_type"];
  801. if([value_type isEqualToString:@"float"])
  802. text=[NSString stringWithFormat:@"%.2f",[text doubleValue] ];
  803. // text =[NSString stringWithFormat:@"%@%@",prefix_str,text] ;
  804. if([self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"format"] isEqualToString:@"price"])
  805. {
  806. text =textView.text;
  807. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  808. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  809. NSNumber * nprice =[NSNumber numberWithDouble: [text doubleValue]];
  810. if(text.length==0)
  811. nprice=nil;
  812. text = [formatter stringFromNumber:nprice];
  813. text=[text stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  814. // textView.text = newprice;
  815. // return NO;
  816. }
  817. textView.text = text;
  818. if(text==nil)
  819. text=@"";
  820. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = text;
  821. NSDictionary* action = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"action"];
  822. NSArray* keys=[action allKeys];
  823. for(int k=0;k<keys.count;k++)
  824. {
  825. NSMutableDictionary* act_k = action[keys[k] ];
  826. if (![self checkActionCondition:act_k[@"condition"]])
  827. continue;
  828. for(int i=0;i<[act_k[@"count"] intValue];i++)
  829. {
  830. NSMutableDictionary * control = act_k [[NSString stringWithFormat:@"item_%d",i] ];
  831. UITextView* tv = (UITextView*) [self findControl:control[@"control"]];
  832. NSString* value = control[@"value"];
  833. NSString* newvalue = nil;
  834. if([value isEqualToString:@"*0.1"])
  835. {
  836. // double f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] doubleValue]*0.1;
  837. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  838. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  839. NSString* prefix_str= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  840. if(prefix_str.length==0)
  841. prefix_str = @"";
  842. if(text.length==0)
  843. {
  844. newvalue=nil;
  845. }
  846. else
  847. {
  848. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  849. double f = [[formatter numberFromString:text] doubleValue]*0.1;
  850. // if(tv==self.hotTextView)
  851. // {
  852. //
  853. // newvalue = [NSString stringWithFormat:@"%.2f",f];
  854. // }
  855. // else
  856. {
  857. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  858. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  859. // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  860. }
  861. }
  862. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  863. }
  864. else if([value isEqualToString:@"*0.9"])
  865. {
  866. // double f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] doubleValue]*0.9;
  867. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  868. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  869. NSString* prefix_str= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  870. if(prefix_str.length==0)
  871. prefix_str = @"";
  872. if(text.length==0)
  873. {
  874. newvalue=nil;
  875. }
  876. else
  877. {
  878. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  879. double f = [[formatter numberFromString:text] doubleValue]*0.9;
  880. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  881. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  882. }
  883. // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  884. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  885. }
  886. else if([value isEqualToString:@"control_6-control_7"])
  887. {
  888. UITextView* tv6 = (UITextView*) [self findControl:@"control_6"];
  889. // float f6= [[tv6.text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue];
  890. //
  891. // float f7= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue];
  892. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  893. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  894. NSString* prefix_str= self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  895. if(prefix_str.length==0)
  896. prefix_str = @"";
  897. NSString * text6=[tv6.text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  898. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  899. double f6 =0.0;
  900. // if(tv6==self.hotTextView)
  901. // f6=[tv6.text doubleValue];
  902. // else
  903. f6=[[formatter numberFromString:text6] doubleValue];
  904. double f7 = [[formatter numberFromString:text] doubleValue];
  905. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f6-f7]];
  906. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  907. // newvalue = [NSString stringWithFormat:@"$ %.2f",f6-f7];
  908. self.controlTemplate [control[@"control"] ][@"value"] = newvalue;
  909. }
  910. tv.text = newvalue;
  911. }
  912. }
  913. // int count = [self.controlTemplate[@"count"] intValue];
  914. // for(int i=0;i<count;i++)
  915. // {
  916. // NSMutableDictionary * control =;
  917. //[self update_newprice];
  918. }
  919. /*
  920. #pragma mark - Navigation
  921. // In a storyboard-based application, you will often want to do a little preparation before navigation
  922. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  923. // Get the new view controller using [segue destinationViewController].
  924. // Pass the selected object to the new view controller.
  925. }
  926. */
  927. #pragma mark signature clicked
  928. //touchimageview Delegate
  929. - (void)TouchImageViewOnTouche:(TouchImageView *)touchImageView
  930. {
  931. NSLog(@"signature button clicked;");
  932. __weak __typeof(self)weakSelf = self;
  933. {
  934. long index = touchImageView.tag - CONTROL_BASE;
  935. NSMutableDictionary* control_json = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ];
  936. // CGRect cellrect_screen = [RAUtils relativeFrameForScreenWithView:trigger];
  937. //
  938. //
  939. // CGRect rect1=[self.view convertRect:cellrect_screen fromView:[[[UIApplication sharedApplication] delegate] window]];
  940. //
  941. // DebugLog(@"convert1 %@",NSStringFromCGRect(rect1));
  942. SignatureListViewController *signatureVC = [ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureListViewController"];
  943. signatureVC.title=@"Signatures";
  944. signatureVC.signatureData = self.signatureData;
  945. signatureVC.subType = control_json[@"name"];
  946. signatureVC.can_fill= [control_json[@"can_fill"] boolValue];
  947. signatureVC.signature_ratio= touchImageView.frame.size;
  948. // signatureVC.imageView = touchImageView;
  949. signatureVC.blk_Select =^(NSString* file)
  950. {
  951. // NSString* file_name=[file lastPathComponent];
  952. NSData* img_data=[ImageUtils load_img:file];
  953. if(img_data!=nil)
  954. {
  955. UIImage* image=[UIImage imageWithData:img_data];
  956. touchImageView.image = image;
  957. long index = touchImageView.tag - CONTROL_BASE;
  958. weakSelf.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  959. }
  960. };
  961. signatureVC.blk_Fill =^(NSString* file,bool fillFullDoc)
  962. {
  963. if(weakSelf.blk_Fill)
  964. weakSelf.blk_Fill(file,control_json[@"name"],fillFullDoc);
  965. // for(int j=0;j<[self.controlTemplate[@"count"] intValue];j++)
  966. // {
  967. // NSMutableDictionary* dest =self.controlTemplate[[NSString stringWithFormat:@"control_%d",j]];
  968. // NSString* type = dest[@"type"];
  969. // if(![type isEqualToString:@"Signature"])
  970. // continue;
  971. // if([dest[@"name"] isEqualToString:control_json[@"name"]])
  972. // {
  973. // dest[@"value"]=file;
  974. // }
  975. // }
  976. [weakSelf refreshControl];
  977. // // NSString* file_name=[file lastPathComponent];
  978. //
  979. // NSData* img_data=[ImageUtils load_img:file];
  980. //
  981. // if(img_data!=nil)
  982. // {
  983. //
  984. // UIImage* image=[UIImage imageWithData:img_data];
  985. // touchImageView.image = image;
  986. //
  987. // long index = touchImageView.tag - CONTROL_BASE;
  988. // self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  989. //
  990. // }
  991. };
  992. signatureVC.blk_Add = ^()
  993. {
  994. __block UIImage* signimg=nil;
  995. SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"];
  996. vc.title =[NSString stringWithFormat:@"Signature: %@",control_json[@"name"]];
  997. vc.signature_ratio= touchImageView.frame.size;
  998. vc.linewidth = [control_json[@"linewidth"] intValue];
  999. vc.onReturnImg = ^(UIImage* img)
  1000. {
  1001. signimg = img;
  1002. if(signimg!=nil)
  1003. {
  1004. NSString* file=[PDFUtils addSignature:signimg to:self.signatureData subType:control_json[@"name"]];
  1005. long index = touchImageView.tag - CONTROL_BASE;
  1006. self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  1007. touchImageView.image = signimg;
  1008. }
  1009. };
  1010. // orderinfoVC.url_type = URL_REMOTE;
  1011. // orderinfoVC.request_url=URL_CARTDELIVERY;
  1012. //
  1013. // orderinfoVC.params = params;
  1014. //
  1015. // orderinfoVC.delegate=self;
  1016. //
  1017. // if(checked.count==count)
  1018. // {
  1019. // orderinfoVC.have_tail = true
  1020. // }
  1021. [self.navigationController pushViewController:vc animated:true];
  1022. };
  1023. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:signatureVC];
  1024. navi.modalPresentationStyle=UIModalPresentationPopover;
  1025. UIPopoverPresentationController* popPc = navi.popoverPresentationController;
  1026. popPc.permittedArrowDirections = UIPopoverArrowDirectionAny;
  1027. popPc.sourceView = touchImageView;
  1028. popPc.delegate = nil;
  1029. [self presentViewController:navi animated:true completion:nil];
  1030. //// menu.selector = self.selector;
  1031. ////
  1032. //// menu.selectordelegate = self;
  1033. //
  1034. // // 1.创建一个UIPopover
  1035. // UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:[[UINavigationController alloc] initWithRootViewController:menu]];
  1036. //
  1037. //
  1038. //
  1039. // UIPopoverPresentationController
  1040. // // 2.设置尺寸
  1041. // // popover.popoverContentSize = CGSizeMake(320, 44 * 5);
  1042. //
  1043. // // 3.从哪里显示出来 --> 指向item
  1044. //
  1045. // [popover presentPopoverFromRect:self.view.frame inView:self.view permittedArrowDirections:0 animated:YES];
  1046. // // [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  1047. //
  1048. //// self.popover = popover;
  1049. // popover pop
  1050. //
  1051. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  1052. }
  1053. // __block int tag = touchImageView.tag;
  1054. // UIViewController* vc=[RAUtils getViewController :touchImageView];
  1055. //
  1056. // if(self.editable==true)
  1057. // {
  1058. //
  1059. //
  1060. // ImageUploadViewController * uploadvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"ImageUploadViewController"];
  1061. //
  1062. // // UIImage* img =[self.buttonImg backgroundImageForState:UIControlStateNormal];;
  1063. //
  1064. // if(self.img_validate)
  1065. // uploadvc.img= touchImageView.image;
  1066. //
  1067. // uploadvc.returnValue = ^(NSString* url_down,NSString* url_up,UIImage* img)
  1068. // {
  1069. //
  1070. // self.imgs[tag] = url_up;
  1071. //
  1072. // NSString* newurl=[RAUtils arr2string:self.imgs separator:@"," trim:false];
  1073. //
  1074. // touchImageView.image=img;
  1075. //
  1076. // if(self.imgChanged)
  1077. // self.imgChanged(url_down,newurl,tag,url_up);
  1078. //
  1079. // };
  1080. //
  1081. // [vc.navigationController pushViewController:uploadvc animated:false];
  1082. // }
  1083. // else
  1084. // {
  1085. // if(touchImageView.image==nil)
  1086. // return ;
  1087. // ImageViewController * imagevc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"ImageViewController"];
  1088. //
  1089. //
  1090. // UIImage* img=touchImageView.image;
  1091. //
  1092. // if(self.img_validate)
  1093. // imagevc.image = img;//.imageView.image = [self.buttonImg backgroundImageForState:UIControlStateNormal];
  1094. //
  1095. // // uploadvc.returnValue = ^(NSString* url_down,NSString* url_up,UIImage* img)
  1096. // // {
  1097. // //
  1098. // // [self.buttonImg setBackgroundImage:img forState:UIControlStateNormal];
  1099. // //
  1100. // // if(self.imgChanged)
  1101. // // self.imgChanged(url_down,url_up);
  1102. // //
  1103. // // };
  1104. //
  1105. // [vc.navigationController pushViewController:imagevc animated:false];
  1106. // }
  1107. // // bundleVC.content_data = self.bundle_item;
  1108. //
  1109. }
  1110. #pragma mark Responding to keyboard events
  1111. - (CGRect)relativeFrameForScreenWithView:(UIView *)v
  1112. {
  1113. UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
  1114. CGRect rect=[v convertRect: v.bounds toView:window];
  1115. return rect;
  1116. // BOOL iOS7 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 7;
  1117. //
  1118. // CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
  1119. // if (!iOS7) {
  1120. // screenHeight -= 20;
  1121. // }
  1122. // UIView *view = v;
  1123. // CGFloat x = .0;
  1124. // CGFloat y = .0;
  1125. // while (view.frame.size.width != 320 || view.frame.size.height != screenHeight) {
  1126. // x += view.frame.origin.x;
  1127. // y += view.frame.origin.y;
  1128. // view = view.superview;
  1129. // if ([view isKindOfClass:[UIScrollView class]]) {
  1130. // x -= ((UIScrollView *) view).contentOffset.x;
  1131. // y -= ((UIScrollView *) view).contentOffset.y;
  1132. // }
  1133. // }
  1134. // return CGRectMake(x, y, v.frame.size.width, v.frame.size.height);
  1135. }
  1136. - (void)keyboardWillChangeFrame:(NSNotification *)notification {
  1137. NSLog(@"keyboardWillChangeFrame");
  1138. }
  1139. - (void)keyboardWillShow:(NSNotification *)notification {
  1140. NSLog(@"keyboardWillShow");
  1141. if(self.keyboard_show)
  1142. return;
  1143. self.keyboard_show=true;
  1144. /*
  1145. Reduce the size of the text view so that it's not obscured by the keyboard.
  1146. Animate the resize so that it's in sync with the appearance of the keyboard.
  1147. */
  1148. NSDictionary *userInfo = [notification userInfo];
  1149. NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  1150. CGSize keyboardSize = [aValue CGRectValue].size;
  1151. NSLog(@"keyboard height:%f",keyboardSize.height);
  1152. CGRect cellrect_screen = [self relativeFrameForScreenWithView:self.hotTextView];
  1153. CGRect rect_screen = [ UIScreen mainScreen ].bounds;
  1154. int cellpos = cellrect_screen.origin.y+cellrect_screen.size.height;
  1155. if(cellpos>rect_screen.size.height-keyboardSize.height)
  1156. {
  1157. // self.resize = true;
  1158. self.keyboard_h =keyboardSize.height;
  1159. CGPoint contentOffsetPoint = self.pdfScrollView.contentOffset;
  1160. CGSize contentSize =self.pdfScrollView.contentSize;//frame.size;
  1161. CGSize size1 = self.pdfScrollView.bounds.size;
  1162. NSLog(@"frame:%@ bound:%@", NSStringFromCGSize(contentSize),NSStringFromCGSize(size1) );
  1163. contentSize.height+=self.keyboard_h;
  1164. contentOffsetPoint.y+=self.keyboard_h;
  1165. self.pdfScrollView.contentSize =contentSize;
  1166. self.pdfScrollView.contentOffset=contentOffsetPoint;
  1167. }
  1168. // self.pdfScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.keyboard_h);
  1169. // CGSize tablecontent =self.editorTable.contentSize;
  1170. // tablecontent.height=tablecontent.height+self.keyboard_h;
  1171. // self.editorTable.contentSize=tablecontent;
  1172. // }
  1173. // Animate the resize of the text view's frame in sync with the keyboard's appearance.
  1174. // [self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration];
  1175. }
  1176. - (void)keyboardWillHide:(NSNotification *)notification {
  1177. NSLog(@"keyboardWillHide");
  1178. self.keyboard_show=false;
  1179. // NSDictionary* userInfo = [notification userInfo];
  1180. /*
  1181. Restore the size of the text view (fill self's view).
  1182. Animate the resize so that it's in sync with the disappearance of the keyboard.
  1183. */
  1184. // NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  1185. // NSTimeInterval animationDuration;
  1186. // [animationDurationValue getValue:&animationDuration];
  1187. CGPoint contentOffsetPoint = self.pdfScrollView.contentOffset;
  1188. CGSize contentSize =self.pdfScrollView.contentSize;//frame.size;
  1189. contentSize.height-=self.keyboard_h;
  1190. contentOffsetPoint.y-=self.keyboard_h;
  1191. self.pdfScrollView.contentSize=contentSize;
  1192. self.pdfScrollView.contentOffset=contentOffsetPoint;
  1193. // CGSize tablecontent =self.pdfScrollView.contentSize;
  1194. // tablecontent.height=tablecontent.height-self.keyboard_h;
  1195. // self.editorTable.contentSize=tablecontent;
  1196. // self.pdfScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
  1197. self.keyboard_h= 0;
  1198. // NSLog(@"before refresh %@",NSStringFromCGSize(self.editorTable.contentSize));
  1199. // if(self.resize)
  1200. // {
  1201. // NSTimeInterval animationDuration = 0.30f;
  1202. // CGRect frame = self.view.frame;
  1203. // // if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
  1204. // // { //还原界面
  1205. // // moveY = prewMoveY;
  1206. // frame.origin.y +=self.ioffset;
  1207. // frame.size. height -=self.ioffset;
  1208. // self.view.frame = frame;
  1209. // // }
  1210. // //self.view移回原位置
  1211. // [UIView beginAnimations:@"ResizeView" context:nil];
  1212. // [UIView setAnimationDuration:animationDuration];
  1213. // self.view.frame = frame;
  1214. // [UIView commitAnimations];
  1215. // //[textField resignFirstResponder];
  1216. // self.ioffset=0;
  1217. // }
  1218. // [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];
  1219. }
  1220. @end