PageViewController.m 63 KB

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