TemplateViewController.m 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  1. //
  2. // TemplateViewController.m
  3. // Granite Expo eSign
  4. //
  5. // Created by Ray on 23/09/2017.
  6. // Copyright © 2017 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "TemplateViewController.h"
  9. #define NUMBERS @"0123456789.\n"
  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 "AppDelegate.h"
  21. typedef enum {
  22. ScreenOrientationUnknown = 0,
  23. ScreenOrientationPortrait = 1,
  24. ScreenOrientationLand = 3
  25. }ScreenOrientation;
  26. @interface TemplateViewController ()
  27. @property (nonatomic,assign) ScreenOrientation orientation;
  28. @property (nonatomic,assign) ScreenOrientation currentAppOrientation;
  29. @end
  30. @implementation TemplateViewController
  31. - (BOOL)canBecomeFirstResponder{
  32. return YES;
  33. }
  34. //- (IBAction)onBtnClick:(id)sender {
  35. // [self becomeFirstResponder];
  36. // UIView* v = sender;
  37. //
  38. // UIMenuItem *detail = [[UIMenuItem alloc] initWithTitle:@"abc" action:@selector(showDetail:)];
  39. // // [detail setValue:_id forKey:@"_id"];
  40. //
  41. // UIMenuController *menu = [UIMenuController sharedMenuController];
  42. //
  43. // [menu setMenuItems:[NSArray arrayWithObjects:detail, nil]];
  44. //
  45. // [menu setTargetRect:v.frame inView:v.superview];
  46. // [menu setMenuVisible:YES animated:YES];
  47. //}
  48. - (ScreenOrientation)currentAppOrientation {
  49. CGSize screenSize = [UIScreen mainScreen].bounds.size;
  50. if (screenSize.width > screenSize.height) return ScreenOrientationLand;
  51. return ScreenOrientationPortrait;
  52. }
  53. - (void)viewDidLoad {
  54. [super viewDidLoad];
  55. //
  56. // self.pdfScrollView = [[PDFScrollView alloc]initWithFrame:CGRectMake(0, 64, 768, 960)];
  57. // self.pdfScrollView.backgroundColor= [UIColor redColor];
  58. //self.edgesForExtendedLayout = UIRectEdgeNone;
  59. // self.pageIndex=1;
  60. // self.pdfPageView.pageIndex=self.pageIndex;
  61. self.pdfPageView.pageRef= self.pageRef;
  62. self.pdfScrollView.contentSize = self.pdfScrollView.frame.size;
  63. self.keyboard_h=0;
  64. // self.pdfScrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
  65. // [self initControl];
  66. // UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
  67. // initWithTarget:self
  68. // action:@selector(handlePinch:)];
  69. //
  70. // [self.view addGestureRecognizer:pinchGestureRecognizer];
  71. // Do any additional setup after loading the view.
  72. self.orientation = ScreenOrientationUnknown;
  73. // self.pdfScrollView.backgroundColor = [UIColor redColor];
  74. // [self.pdfScrollView viewWithTag:1024].backgroundColor = [UIColor orangeColor];
  75. // self.pdfPageView.backgroundColor = [UIColor yellowColor];
  76. // self.editControlView.backgroundColor = [UIColor colorWithRed:0.4 green:0.8 blue:0.3 alpha:0.5];
  77. //
  78. // [self.pdfPageView removeFromSuperview];
  79. // [self.editControlView removeFromSuperview];
  80. // self.view.backgroundColor = [UIColor blueColor];
  81. }
  82. -(void)viewWillAppear:(BOOL)animated
  83. {
  84. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  85. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  86. // 键盘高度变化通知,ios5.0新增的
  87. #ifdef __IPHONE_5_0
  88. float version = [[[UIDevice currentDevice] systemVersion] floatValue];
  89. if (version >= 5.0) {
  90. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  91. }
  92. #endif
  93. // self.pdfScrollView.contentSize = self.pdfScrollView.frame.size;
  94. }
  95. - (void)viewDidAppear:(BOOL)animated {
  96. [super viewDidAppear:animated];
  97. if (self.orientation != ScreenOrientationUnknown) {
  98. if (self.orientation == self.currentAppOrientation) {
  99. // 修复未完全显示时停止拖动,再进入该页面就不会生成Control控件
  100. if (self.editControlView.subviews.count) {
  101. [self refreshControl];
  102. } else {
  103. [self initControl];
  104. }
  105. } else {
  106. [self rotateView];
  107. }
  108. } else {
  109. [self initControl];
  110. }
  111. [self.parentViewController.view layoutIfNeeded];
  112. if (self.controlUpdateDelegate) {
  113. [self.controlUpdateDelegate currentShowViewController:self];
  114. }
  115. }
  116. - (void)viewWillLayoutSubviews {
  117. /*
  118. * 解决旋转后第二个视图覆盖部分当前视图
  119. */
  120. [self clearControlView];
  121. self.pdfScrollView.scrollEnabled = NO;
  122. // root
  123. [self updateView:self.view];
  124. // scroll
  125. [self updateView:self.pdfScrollView];
  126. // 1024 view
  127. [self updateView:[self.pdfScrollView viewWithTag:1024]];
  128. // pdf
  129. [self updateView:self.pdfPageView];
  130. // NSLog(@"pdf page frame %@",[NSValue valueWithCGRect:self.pdfPageView.frame]);
  131. [self.pdfPageView setNeedsDisplay];
  132. // edit
  133. [self updateView:self.editControlView];
  134. // contentSize
  135. self.pdfScrollView.contentSize = [self.pdfScrollView viewWithTag:1024].bounds.size;
  136. self.pdfScrollView.contentOffset = CGPointZero;
  137. [self initControl];
  138. [self.view setNeedsDisplay];
  139. self.pdfScrollView.scrollEnabled = YES;
  140. }
  141. - (void)clearControlView {
  142. for (UIView * v in self.editControlView.subviews)
  143. {
  144. [v removeFromSuperview ];
  145. }
  146. }
  147. - (void)rotateView {
  148. if (self.orientation == self.currentAppOrientation) return;
  149. [self viewWillLayoutSubviews];
  150. self.orientation = self.currentAppOrientation;
  151. for (UIViewController *vc in self.parentViewController.childViewControllers) {
  152. [vc viewWillLayoutSubviews];
  153. }
  154. }
  155. - (void)updateView:(UIView *)view {
  156. // return;
  157. if (!view) return;
  158. CGRect frame = [UIScreen mainScreen].bounds;
  159. if (view == self.view) {
  160. } else if (view == self.pdfScrollView) {
  161. frame.origin.y = 64;
  162. frame.size.height -= 64;
  163. self.pdfScrollView.contentSize = CGSizeZero;
  164. } else {
  165. frame.size.height -= 64;
  166. view.transform = CGAffineTransformIdentity; // 解决缩放后,旋转屏幕。产生偏差(ContentSize和ContentOffset)
  167. }
  168. view.frame = frame;
  169. view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
  170. [view layoutIfNeeded];
  171. [view setNeedsDisplay];
  172. }
  173. -(void) viewWillDisappear:(BOOL)animated
  174. {
  175. [self.hotTextView endEditing:true];
  176. [[NSNotificationCenter defaultCenter] removeObserver:self];
  177. self.orientation = self.currentAppOrientation;
  178. }
  179. -(void) dealloc
  180. {
  181. // CFBridgingRelease(self.pageRef);
  182. }
  183. - (void)didReceiveMemoryWarning {
  184. [super didReceiveMemoryWarning];
  185. // Dispose of any resources that can be recreated.
  186. }
  187. //- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer
  188. //{
  189. //// recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
  190. //// recognizer.scale = 1;
  191. //
  192. // CGSize contentsize =self.pdfScrollView.contentSize;
  193. //
  194. // self.pdfScrollView.contentSize = CGSizeMake(contentsize.width*recognizer.scale, contentsize.height*recognizer.scale);
  195. //}
  196. //+(CGRect)WindowRect2PDFRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size
  197. //{
  198. // CGRect pdf_expand;
  199. // float offset_x = 0;
  200. // float offset_y = 0;
  201. // float scale =1;
  202. // if(pdf_rect.size.height>=pdf_rect.size.width)
  203. // {
  204. // pdf_expand = CGRectMake(0, 0, pdf_rect.size.height*window_size.width/window_size.height, pdf_rect.size.height);
  205. // offset_x = (pdf_expand.size.width-pdf_rect.size.width)/2;
  206. // scale = pdf_rect.size.height/window_size.height;
  207. //
  208. // }
  209. // else
  210. // {
  211. // pdf_expand = CGRectMake(0, 0, pdf_rect.size.width, pdf_rect.size.width*window_size.height/window_size.width);
  212. // offset_y = (pdf_expand.size.height-pdf_rect.size.height)/2;
  213. // scale = pdf_rect.size.width/window_size.width;
  214. // }
  215. // float x=rect.origin.x*scale-offset_x;
  216. // float y=rect.origin.y*scale-offset_y;
  217. // float width = rect.size.width*scale;
  218. // float height = rect.size.height*scale;
  219. //
  220. // return CGRectMake(x, y, width, height);
  221. //
  222. //}
  223. -(CGRect) scaleControl:(CGRect) frame from:(CGSize)fwindowsize to:(CGSize)twindowsize
  224. {
  225. CGRect pdfsize=CGPDFPageGetBoxRect(self.pageRef, kCGPDFMediaBox);
  226. frame=[PDFUtils WindowRect2PDFRect:frame pdf_rect:pdfsize window_size:fwindowsize];
  227. frame= [PDFUtils PDFRect2WindowRect:frame pdf_rect:pdfsize window_size:twindowsize];
  228. return frame;
  229. }
  230. -(void) initControl
  231. {
  232. if(self.hide_control)
  233. return;
  234. // return;
  235. [self clearControlView]; // fill all时未显示。
  236. int count = [self.page_controlTemplate[@"count"] intValue];
  237. for(int i=0;i<count;i++)
  238. {
  239. NSMutableDictionary * control =self.page_controlTemplate [[NSString stringWithFormat:@"control_%d",i] ];
  240. if([control[@"type"] isEqualToString:@"TextView"])
  241. {
  242. [self addTextView:control destView:self.editControlView index:i];
  243. }
  244. else if([control[@"type"] isEqualToString:@"Button"])
  245. {
  246. [self addButton:control destView:self.editControlView index:i];
  247. }
  248. else if([control[@"type"] isEqualToString:@"Check"])
  249. {
  250. [self addCheck:control destView:self.editControlView index:i];
  251. }
  252. else if([control[@"type"] isEqualToString:@"Signature"])
  253. {
  254. [self addSignatureButton:control destView:self.editControlView index:i];
  255. }
  256. else if([control[@"type"] isEqualToString:@"Image"])
  257. {
  258. [self addImage:control destView:self.editControlView index:i];
  259. }
  260. else if([control[@"type"] isEqualToString:@"Label"])
  261. {
  262. [self addLabel:control destView:self.editControlView index:i];
  263. }
  264. else if([control[@"type"] isEqualToString:@"DatePicker"])
  265. {
  266. [self addDatePicker:control destView:self.editControlView index:i];
  267. }
  268. }
  269. }
  270. -(void) refreshControl
  271. {
  272. int count = [self.page_controlTemplate[@"count"] intValue];
  273. for(int i=0;i<count;i++)
  274. {
  275. NSString* controlname =[NSString stringWithFormat:@"control_%d",i];
  276. NSMutableDictionary * control =self.page_controlTemplate [ controlname];
  277. // if([control[@"type"] isEqualToString:@"TextView"])
  278. // {
  279. // UITextView* tv = (UITextView*) [self findControl:controlname];
  280. // tv.text = control[@"value"];
  281. // }
  282. // else if([control[@"type"] isEqualToString:@"Button"])
  283. // {
  284. // [self addButton:control destView:self.editControlView index:i];
  285. //
  286. // }
  287. // else if([control[@"type"] isEqualToString:@"Check"])
  288. // {
  289. // [self addCheck:control destView:self.editControlView index:i];
  290. //
  291. // }
  292. // else
  293. if([control[@"type"] isEqualToString:@"Signature"])
  294. {
  295. TouchImageView* btn = (TouchImageView*) [self findControl:controlname];
  296. NSString* file =control[@"value"];
  297. NSData* img_data=[ImageUtils load_img:file];
  298. if(img_data!=nil)
  299. {
  300. UIImage* image=[UIImage imageWithData:img_data];
  301. btn.image = image;
  302. }
  303. }
  304. // else if([control[@"type"] isEqualToString:@"Image"])
  305. // {
  306. // [self addImage:control destView:self.editControlView index:i];
  307. //
  308. // }
  309. // else if([control[@"type"] isEqualToString:@"Label"])
  310. // {
  311. // [self addLabel:control destView:self.editControlView index:i];
  312. //
  313. // }
  314. // else if([control[@"type"] isEqualToString:@"DatePicker"])
  315. // {
  316. // [self addDatePicker:control destView:self.editControlView index:i];
  317. //
  318. // }
  319. }
  320. }
  321. -(void)EndEditing
  322. {
  323. [[self view] endEditing:YES];
  324. }
  325. -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  326. {
  327. if (self.presentedViewController) {
  328. [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
  329. }
  330. }
  331. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  332. [self rotateView];// 在此重新布局使视图在旋转过程中减少卡顿,更自然。
  333. }
  334. -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  335. {
  336. // [self rotateView];
  337. // [self initControl];
  338. //// [self.pdfPageView setNeedsLayout];
  339. // [self.pdfPageView setNeedsDisplay];
  340. //// UIDeviceOrientation dev_ori = [[UIDevice currentDevice]orientation];
  341. //
  342. //// UIApplication * app = [UIApplication sharedApplication];
  343. //// AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  344. //// [appDelegate didRotated];
  345. //
  346. // // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  347. // // if (UIInterfaceOrientationIsLandscape(orientation)|| UIInterfaceOrientationIsPortrait(orientation))
  348. // // {
  349. // // self.orientation = orientation;
  350. // // [self.collectionview.collectionViewLayout invalidateLayout];
  351. // // DebugLog(@"routed");
  352. // // }
  353. //
  354. }
  355. #pragma mark add controls
  356. -(bool) checkActionCondition:(NSString*) condition
  357. {
  358. if([condition isEqualToString:@"true"])
  359. return true;
  360. if([condition isEqualToString:@"false"])
  361. return false;
  362. if([condition isEqualToString:@"control_18:[value]==0"])
  363. {
  364. NSArray* arr= self.page_controlTemplate[@"control_18"][@"value"];
  365. if(arr.count==0)
  366. return false;
  367. else
  368. return [arr[0] intValue]==0;
  369. }
  370. return false;
  371. }
  372. -(UIView*) findControl:(NSString*)controlName
  373. {
  374. controlName=[controlName stringByReplacingOccurrencesOfString:@"control_" withString:@""];
  375. int d_idx = [controlName intValue];
  376. return [self.view viewWithTag:d_idx+CONTROL_BASE];
  377. }
  378. -(UITextView*) findNextTV:(UITextView*)tv
  379. {
  380. long index = tv.tag - CONTROL_BASE;
  381. int count = [self.page_controlTemplate[@"count"] intValue];
  382. for(long i=index+1;i<count;i++)
  383. {
  384. NSMutableDictionary * control =self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",i] ];
  385. if(![control[@"type"] isEqualToString:@"TextView"])
  386. {
  387. continue;
  388. }
  389. for(UIView*v in self.editControlView.subviews)
  390. {
  391. if(v.tag==i+CONTROL_BASE)
  392. return (UITextView*)v;
  393. }
  394. }
  395. return nil;
  396. }
  397. -(void) addTextView:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  398. {
  399. 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];
  400. UITextView* tv = [[UITextView alloc] initWithFrame:srect];
  401. if([template[@"next_end"] boolValue])
  402. tv.returnKeyType=UIReturnKeyDefault;
  403. else
  404. tv.returnKeyType=UIReturnKeyNext;
  405. float fontsize = [template[@"size"] floatValue];
  406. if(fontsize==0)
  407. fontsize=10;
  408. if(self.currentAppOrientation==ScreenOrientationLand)
  409. fontsize=fontsize*0.8; // 缩小字体否则textview显示不下。
  410. [tv setFont:[UIFont systemFontOfSize:fontsize]];
  411. bool isdisable = [template[@"disable"] boolValue];
  412. // tv.editable = !isdisable;
  413. tv.editable = true; // for update frame
  414. NSString* text= template[@"value"];
  415. if(text.length==0)
  416. text=@" ";
  417. tv.text =text;
  418. float tv_vmargin= [template[@"tv_vmargin"] floatValue];
  419. NSString* capital= template[@"capital"];
  420. UITextAutocapitalizationType capitalType=UITextAutocapitalizationTypeWords;
  421. if([capital isEqualToString:@"charater"])
  422. {
  423. capitalType=UITextAutocapitalizationTypeAllCharacters;
  424. }
  425. else if([capital isEqualToString:@"sentences"])
  426. {
  427. capitalType=UITextAutocapitalizationTypeSentences;
  428. }
  429. else if([capital isEqualToString:@"none"])
  430. {
  431. capitalType=UITextAutocapitalizationTypeNone;
  432. }
  433. else
  434. {
  435. capitalType=UITextAutocapitalizationTypeWords;
  436. }
  437. tv.autocapitalizationType=capitalType;
  438. NSMutableAttributedString* attr_str= [tv.attributedText mutableCopy];
  439. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  440. if(tv_vmargin!=0)
  441. paragraphStyle.lineHeightMultiple = tv_vmargin;
  442. // paragraphStyle.maximumLineHeight = 25.f;
  443. // paragraphStyle.minimumLineHeight = 15.f;
  444. // paragraphStyle.firstLineHeadIndent = 20.f;
  445. // paragraphStyle.alignment = NSTextAlignmentJustified;
  446. [attr_str addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,attr_str.length)];
  447. tv.attributedText=attr_str;
  448. if([text isEqualToString:(@" ")])
  449. tv.text=@"";
  450. tv.tag = index+ CONTROL_BASE;
  451. tv.scrollEnabled=false;
  452. NSString* tv_format=template[@"keyboard"];
  453. if([tv_format isEqualToString:@"tel"])
  454. {
  455. tv.keyboardType=UIKeyboardTypePhonePad;
  456. }
  457. else if([tv_format isEqualToString:@"number"])
  458. {
  459. tv.keyboardType=UIKeyboardTypeNumberPad;
  460. }
  461. else if([tv_format isEqualToString:@"email"])
  462. {
  463. tv.keyboardType=UIKeyboardTypeEmailAddress;
  464. }
  465. else
  466. {
  467. tv.keyboardType=UIKeyboardTypeDefault;
  468. }
  469. //UIColorFromARGB(0x4066ccff);
  470. tv.backgroundColor = UIColorFromRGB(TV_BG);//[UIColor lightGrayColor];
  471. // tv.place
  472. // [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  473. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  474. tv.delegate = self;
  475. // [btn addTarget:self action:@selector(ControlButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  476. if(DEBUG_PDFSIG)
  477. {
  478. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  479. }
  480. [destView addSubview:tv];
  481. }
  482. -(void) addLabel:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  483. {
  484. 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];
  485. UILabel* label = [[UILabel alloc] initWithFrame:srect];
  486. float fontsize = [template[@"size"] floatValue];
  487. if(fontsize==0)
  488. fontsize=10;
  489. [label setFont:[UIFont systemFontOfSize:fontsize]];
  490. NSString * alignment=template[@"textAlignment"];
  491. if([alignment.lowercaseString isEqualToString:@"center"])
  492. label.textAlignment=NSTextAlignmentCenter;
  493. else if([alignment.lowercaseString isEqualToString:@"left"])
  494. label.textAlignment=NSTextAlignmentLeft;
  495. else if([alignment.lowercaseString isEqualToString:@"right"])
  496. label.textAlignment=NSTextAlignmentRight;
  497. label.text =template[@"value"];
  498. label.tag = index+ CONTROL_BASE;
  499. //UIColorFromARGB(0x4066ccff);
  500. // tv.backgroundColor = UIColorFromRGB(TV_BG);//[UIColor lightGrayColor];
  501. // tv.place
  502. // [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  503. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  504. // tv.delegate = self;
  505. // [btn addTarget:self action:@selector(ControlButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  506. if(DEBUG_PDFSIG)
  507. {
  508. [label setBackgroundColor:[UIColor grayColor]];
  509. }
  510. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizer:)];
  511. [label addGestureRecognizer:tap];
  512. label.userInteractionEnabled = YES;
  513. [destView addSubview:label];
  514. }
  515. -(void) addButton:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  516. {
  517. 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];
  518. UIButton* btn = [[UIButton alloc] initWithFrame:srect];
  519. [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  520. btn.tag = index+ CONTROL_BASE;
  521. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  522. btn.backgroundColor = UIColorFromRGB(BT_BG);
  523. [btn addTarget:self action:@selector(ControlButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  524. if(DEBUG_PDFSIG)
  525. {
  526. // btn.backgroundColor = [UIColor lightGrayColor];
  527. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  528. }
  529. [destView addSubview:btn];
  530. }
  531. -(void) addDatePicker:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  532. {
  533. 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];
  534. UIButton* btn = [[UIButton alloc] initWithFrame:srect];
  535. [btn setTitle:template[@"value"] forState:UIControlStateNormal];
  536. float fontsize = [template[@"size"] floatValue];
  537. if(fontsize==0)
  538. fontsize=10;
  539. if(self.currentAppOrientation==ScreenOrientationLand)
  540. fontsize=fontsize*0.8; // 缩小字体否则textview显示不下。
  541. btn.titleLabel.font = [UIFont systemFontOfSize: fontsize];
  542. btn.tag = index+ CONTROL_BASE;
  543. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  544. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  545. btn.backgroundColor = UIColorFromRGB(BT_BG);
  546. [btn addTarget:self action:@selector(ControlDatePickerClicked:) forControlEvents:UIControlEventTouchUpInside];
  547. if(DEBUG_PDFSIG)
  548. {
  549. // btn.backgroundColor = [UIColor lightGrayColor];
  550. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  551. }
  552. [destView addSubview:btn];
  553. }
  554. -(UIView*) createMarker:(int)size x:(int)x y:(int)y destView:(UIView*)destView
  555. {
  556. CGRect srect=[self scaleControl:CGRectMake(x,y,size,size) from:CGSizeMake(768, 960.5) to:destView.frame.size];
  557. UIView* v=[[UIView alloc] initWithFrame:srect];
  558. return v;
  559. }
  560. -(void) addCheck:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  561. {
  562. 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];
  563. UIButton* btn = [[UIButton alloc] initWithFrame:srect];
  564. // ref addTarget:self action:@selector(manually_refresh) forControlEvents:UIControlEventValueChanged
  565. btn.tag = index+ CONTROL_BASE;
  566. [btn addTarget:self action:@selector(CheckButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  567. UIColor* marker_bg=UIColorFromRGB(CK_BG);//[UIColor clearColor];
  568. if(DEBUG_PDFSIG)
  569. {
  570. btn.backgroundColor = [UIColor lightGrayColor];
  571. btn.alpha = 0.5;
  572. [btn setTitle:template[@"title"] forState:UIControlStateNormal];
  573. // marker_bg = [UIColor redColor];
  574. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  575. }
  576. [destView addSubview:btn];
  577. NSArray* cadedate = template[@"cadedate"];
  578. NSArray* checkedData = self.page_controlTemplate [[NSString stringWithFormat:@"control_%d",index] ][@"value"];
  579. float fontsize = [template[@"size"] floatValue];
  580. if(fontsize==0)
  581. fontsize=10;
  582. // if(checkedData.count>0)
  583. // marker_bg = [UIColor clearColor];
  584. // else
  585. marker_bg=UIColorFromRGB(CK_BG);
  586. for(int i=0;i<cadedate.count;i++)
  587. {
  588. NSArray* item = cadedate[i];
  589. NSArray* checkmarker = item[1];
  590. if(checkmarker.count==0)
  591. {
  592. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  593. btn.titleLabel.font = [UIFont systemFontOfSize: fontsize];
  594. btn.backgroundColor = UIColorFromRGB(CK_BG);
  595. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  596. {
  597. [btn setTitle:item[0][0] forState:UIControlStateNormal];
  598. }
  599. continue;
  600. }
  601. else
  602. {
  603. [btn setTitle:@"" forState:UIControlStateNormal];
  604. }
  605. UIView* marker=[self createMarker:[template[@"marker_size"] intValue] x:[item[1][0] intValue] y:[item[1][1] intValue] destView:destView];
  606. marker.backgroundColor = marker_bg;
  607. marker.tag = CHECK_BASE+index*1000+i;
  608. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  609. {
  610. marker.backgroundColor= UIColorFromRGB(CK_MK);
  611. }
  612. else
  613. {
  614. marker.backgroundColor= marker_bg;
  615. }
  616. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizer:)];
  617. [marker addGestureRecognizer:tap];
  618. [destView addSubview:marker];
  619. }
  620. }
  621. -(void) addSignatureButton:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  622. {
  623. 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];
  624. TouchImageView* btn = [[TouchImageView alloc] initWithFrame:srect];
  625. btn.contentMode = UIViewContentModeScaleAspectFit;
  626. btn.delegate = self;
  627. btn.layer.borderColor = [UIColor clearColor].CGColor;
  628. btn.layer.borderWidth = 0;
  629. btn.tag = index+ CONTROL_BASE;
  630. NSString* file =template[@"value"];
  631. NSData* img_data=[ImageUtils load_img:file];
  632. if(img_data!=nil)
  633. {
  634. UIImage* image=[UIImage imageWithData:img_data];
  635. btn.image = image;
  636. }
  637. else
  638. {
  639. if([template[@"disable"] boolValue]&& [template[@"required"] boolValue])
  640. {
  641. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  642. UIImage* img = [UIImage imageNamed:@"na"];
  643. btn.image = img;
  644. template[@"value"]=appDelegate.na_path;
  645. }
  646. }
  647. btn.backgroundColor = UIColorFromRGB(SIG_BG);
  648. if(DEBUG_PDFSIG)
  649. {
  650. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  651. }
  652. [destView addSubview:btn];
  653. }
  654. -(void) addImage:(NSMutableDictionary*) template destView:(UIView*)destView index:(int) index
  655. {
  656. 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];
  657. UIImageView* btn = [[UIImageView alloc] initWithFrame:srect];
  658. btn.contentMode = UIViewContentModeScaleAspectFit;
  659. btn.layer.borderColor = [UIColor clearColor].CGColor;
  660. btn.layer.borderWidth = 0;
  661. btn.tag = index+ CONTROL_BASE;
  662. NSString* file =template[@"value"];
  663. NSData* img_data=[ImageUtils load_img:file];
  664. if(img_data!=nil)
  665. {
  666. UIImage* image=[UIImage imageWithData:img_data];
  667. btn.image = image;
  668. }
  669. btn.backgroundColor = UIColorFromRGB(SIG_BG);
  670. if(DEBUG_PDFSIG)
  671. {
  672. // [btn setBackgroundImage:[UIColor grayColor] forState:UIControlStateNormal];
  673. }
  674. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizer:)];
  675. [btn addGestureRecognizer:tap];
  676. btn.userInteractionEnabled = YES;
  677. [destView addSubview:btn];
  678. }
  679. #pragma mark UIScrollView delegate
  680. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  681. UIView *subView = [scrollView viewWithTag:1024];
  682. return subView;
  683. }
  684. - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale
  685. {
  686. CGPoint contentOffsetPoint = self.pdfScrollView.contentOffset;
  687. CGSize contentSize =self.pdfScrollView.contentSize;//frame.size;
  688. // CGSize size1 = self.pdfScrollView.bounds.size;
  689. // NSLog(@"frame:%@ bound:%@", NSStringFromCGSize(contentSize),NSStringFromCGSize(size1) );
  690. contentSize.height+=self.keyboard_h;
  691. contentOffsetPoint.y+=self.keyboard_h;
  692. self.pdfScrollView.contentSize =contentSize;
  693. self.pdfScrollView.contentOffset=contentOffsetPoint;
  694. [self.pdfPageView setNeedsDisplay];
  695. }
  696. //- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
  697. //
  698. //
  699. //
  700. //}
  701. #pragma mark Button Click
  702. - (void)ControlDatePickerClicked:(UIButton *)sender {
  703. if (self.controlUpdateDelegate) {
  704. [self.controlUpdateDelegate focusControlView:sender];
  705. return;
  706. }
  707. long index = sender.tag - CONTROL_BASE;
  708. if([self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"disable"] boolValue] )
  709. {
  710. return;
  711. }
  712. __weak __typeof(self)weakSelf = self;
  713. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  714. [formatter setDateFormat:@"MM/dd/yyyy"];
  715. NSString* date = sender.currentTitle;
  716. NSDate* currentDate =nil;
  717. if(date.length==0)
  718. currentDate=[NSDate date];
  719. else
  720. currentDate=[formatter dateFromString:date];
  721. DatePickerViewController* dpvc =[ [UIStoryboard storyboardWithName:@"CommonEditor" bundle:nil] instantiateViewControllerWithIdentifier:@"DatePickerViewController"];
  722. dpvc.pickerMode = UIDatePickerModeDate;
  723. dpvc.date = currentDate;
  724. dpvc.formatter = formatter;
  725. dpvc.labelTime.text = [formatter stringFromDate:currentDate];
  726. dpvc.blk_Set = ^(NSString* strdate)
  727. {
  728. // weakSelf.labelSignDate.text = strdate;
  729. [sender setTitle:strdate forState:UIControlStateNormal];
  730. weakSelf.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = strdate;
  731. weakSelf.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"dirty"]=[NSNumber numberWithBool:true];
  732. };
  733. dpvc.title=@"Please choose a date";
  734. [self.navigationController pushViewController:dpvc animated:true];
  735. }
  736. - (void)ControlButtonClicked:(UIButton *)sender {
  737. if (self.controlUpdateDelegate) {
  738. [self.controlUpdateDelegate focusControlView:sender];
  739. return;
  740. }
  741. // DebugLog(@"cart sort button clicked");
  742. NSLog(@"button clicked;");
  743. // [self.view addSubview:self.sortItemController.view];
  744. }
  745. - (void)CheckButtonClicked:(UIButton *)sender {
  746. // DebugLog(@"cart sort button clicked");
  747. if (self.controlUpdateDelegate) {
  748. [self.controlUpdateDelegate focusControlView:sender];
  749. return;
  750. }
  751. NSLog(@"check clicked;");
  752. long index = sender.tag - CONTROL_BASE;
  753. bool single_select = [self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"single_select"] boolValue];
  754. bool show_detail = [self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"show_detail"] boolValue];
  755. NSArray* rowData = self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"cadedate"];
  756. NSArray* checkedData = self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"];
  757. CheckSelectorViewController *checkVC = [ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"CheckSelectorViewController"];
  758. checkVC.blk_OK = ^(NSArray* checkedData){
  759. for(int i=0;i<rowData.count;i++)
  760. {
  761. long tag=CHECK_BASE+index*1000+i;
  762. NSArray* item = rowData[i];
  763. NSArray* checkmarker = item[1];
  764. // checkmarker 不显示,则显示选项内容文字。
  765. if(checkmarker.count==0)
  766. {
  767. sender.backgroundColor = UIColorFromRGB(CK_BG);
  768. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  769. {
  770. [sender setTitle:item[0][0] forState:UIControlStateNormal];
  771. }
  772. // continue;
  773. }
  774. else
  775. {
  776. [sender setTitle:@"" forState:UIControlStateNormal];
  777. if([checkedData containsObject:[NSNumber numberWithLong:i]])
  778. {
  779. // [checkedData removeObject:[NSNumber numberWithLong:indexPath.row]];
  780. [sender.superview viewWithTag:tag].backgroundColor= [UIColor blackColor];
  781. }
  782. else
  783. {
  784. // [self.checkedData addObject:[NSNumber numberWithLong:indexPath.row] ];
  785. [sender.superview viewWithTag:tag].backgroundColor= [UIColor clearColor];
  786. }
  787. }
  788. bool checked =[checkedData containsObject:[NSNumber numberWithLong:i]];
  789. if(item.count>=3) // 模板有action项
  790. {
  791. //[sender setTitle:item[0][0] forState:UIControlStateNormal];
  792. NSMutableDictionary* action = item[2];
  793. if(checked)
  794. {
  795. NSArray* disable_arr = action[@"disable"] ;
  796. for(int d = 0 ; d<disable_arr.count;d++)
  797. {
  798. self.page_controlTemplate [disable_arr[d] ] [@"disable"] = [NSNumber numberWithBool:true];
  799. NSString* s_idx =disable_arr[d];
  800. s_idx=[s_idx stringByReplacingOccurrencesOfString:@"control_" withString:@""];
  801. int d_idx = [s_idx intValue];
  802. // ((UITextView*)[sender.superview viewWithTag:d_idx+CONTROL_BASE]).editable = false;
  803. ((UITextView*)[sender.superview viewWithTag:d_idx+CONTROL_BASE]).editable = true; // for update frame
  804. }
  805. NSArray* enable_arr = action[@"enable"] ;
  806. for(int e = 0 ; e<enable_arr.count;e++)
  807. {
  808. self.page_controlTemplate [enable_arr[e] ] [@"disable"] = [NSNumber numberWithBool:false];
  809. NSString* s_idx =enable_arr[e];
  810. s_idx=[s_idx stringByReplacingOccurrencesOfString:@"control_" withString:@""];
  811. int d_idx = [s_idx intValue];
  812. ((UITextView*)[sender.superview viewWithTag:d_idx+CONTROL_BASE]).editable = true;
  813. }
  814. NSMutableDictionary* json_setval=action[@"set_val"] ;
  815. for(int i=0;i<[json_setval[@"count"] intValue];i++)
  816. {
  817. NSMutableDictionary * control = json_setval [[NSString stringWithFormat:@"item_%d",i] ];
  818. UITextView* tv = (UITextView*) [self findControl:control[@"control"]];
  819. NSString* value = control[@"value"];
  820. UITextView* tv6 = (UITextView*) [self findControl:@"control_10"];
  821. NSString* text = tv6.text;
  822. NSString* newvalue = nil;
  823. if([value isEqualToString:@"*0.1"])
  824. {
  825. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  826. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  827. NSString* prefix_str= self.page_controlTemplate [control[@"control"] ][@"prefix_str"];
  828. if(prefix_str.length==0)
  829. prefix_str = @"";
  830. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  831. double f = [[formatter numberFromString:text] doubleValue]*0.1;
  832. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  833. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  834. // float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.1;
  835. // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  836. self.page_controlTemplate [control[@"control"] ][@"value"] = newvalue;
  837. self.page_controlTemplate [control[@"control"] ][@"dirty"] = [NSNumber numberWithBool:true];
  838. }
  839. else if([value isEqualToString:@"*0.9"])
  840. {
  841. // float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.9;
  842. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  843. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  844. NSString* prefix_str= self.page_controlTemplate [control[@"control"] ][@"prefix_str"];
  845. if(prefix_str.length==0)
  846. prefix_str = @"";
  847. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  848. double f = [[formatter numberFromString:text] doubleValue]*0.9;
  849. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  850. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  851. // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  852. self.page_controlTemplate [control[@"control"] ][@"value"] = newvalue;
  853. self.page_controlTemplate [control[@"control"] ][@"dirty"] = [NSNumber numberWithBool:true];
  854. }
  855. tv.text = newvalue;
  856. }
  857. }
  858. NSMutableDictionary* json_cd=action[@"conditional_constraint"][checked?@"true":@"false"] ;
  859. {
  860. for(int i=0;i<[json_cd[@"count"] intValue];i++)
  861. {
  862. NSMutableDictionary * destination = json_cd [[NSString stringWithFormat:@"item_%d",i] ];
  863. int page = [destination[@"page"] intValue];
  864. NSString* control = destination[@"control"];
  865. self.doc_controlTemplate[[NSString stringWithFormat:@"page_%d",page]][control][@"disable"]=destination[@"disable"];
  866. id value =destination[@"set_val"];
  867. self.doc_controlTemplate[[NSString stringWithFormat:@"page_%d",page]][control][@"value"]=value;
  868. // UITextView* tv = (UITextView*) [self findControl:destination[@"control"]];
  869. // NSString* value = control[@"value"];
  870. //
  871. // UITextView* tv6 = (UITextView*) [self findControl:@"control_10"];
  872. // NSString* text = tv6.text;
  873. // NSString* newvalue = nil;
  874. // if([value isEqualToString:@"*0.1"])
  875. // {
  876. //
  877. // NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  878. // formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  879. // NSString* prefix_str= self.page_controlTemplate [control[@"control"] ][@"prefix_str"];
  880. // if(prefix_str.length==0)
  881. // prefix_str = @"";
  882. // text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  883. // double f = [[formatter numberFromString:text] doubleValue]*0.1;
  884. // newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  885. //
  886. // newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  887. // // float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.1;
  888. // // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  889. // self.page_controlTemplate [control[@"control"] ][@"value"] = newvalue;
  890. // self.page_controlTemplate [control[@"control"] ][@"dirty"] = [NSNumber numberWithBool:true];
  891. // }
  892. // else if([value isEqualToString:@"*0.9"])
  893. // {
  894. // // float f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue]*0.9;
  895. // NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  896. // formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  897. // NSString* prefix_str= self.page_controlTemplate [control[@"control"] ][@"prefix_str"];
  898. // if(prefix_str.length==0)
  899. // prefix_str = @"";
  900. // text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  901. // double f = [[formatter numberFromString:text] doubleValue]*0.9;
  902. // newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  903. // newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  904. // // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  905. // self.page_controlTemplate [control[@"control"] ][@"value"] = newvalue;
  906. // self.page_controlTemplate [control[@"control"] ][@"dirty"] = [NSNumber numberWithBool:true];
  907. // }
  908. //
  909. //
  910. // tv.text = newvalue;
  911. }
  912. }
  913. }
  914. // UIView* v= sender;
  915. }
  916. self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"]=checkedData;
  917. self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"dirty"] = [NSNumber numberWithBool:true];
  918. };
  919. checkVC.rowData = rowData;
  920. checkVC.checkedData = [checkedData mutableCopy];
  921. checkVC.single_select = single_select;
  922. checkVC.show_detail = show_detail;
  923. checkVC.title=@"abcdefg";
  924. checkVC.modalPresentationStyle = UIModalPresentationFormSheet;
  925. [self presentViewController:checkVC animated:YES completion:nil];
  926. }
  927. #pragma mark TextViewDelegate
  928. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  929. {
  930. long index = textView.tag - CONTROL_BASE;
  931. if([text isEqualToString:@""])
  932. return true;
  933. if([text isEqualToString:@"\n"])
  934. {
  935. // [textView resignFirstResponder];
  936. UITextView * tv = [self findNextTV:textView];
  937. if(tv)
  938. [tv becomeFirstResponder];
  939. [textView resignFirstResponder];
  940. return false;
  941. }
  942. NSString* tv_format=self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"format"];
  943. if([tv_format isEqualToString:@"us_tel"])
  944. {
  945. NSMutableString *newString = [[textView.text stringByReplacingCharactersInRange:range withString:text] mutableCopy];
  946. NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
  947. NSString *decimalString = [components componentsJoinedByString:@""];
  948. NSUInteger length = decimalString.length;
  949. BOOL hasLeadingOne = length > 0 && [decimalString characterAtIndex:0] == '1';
  950. if (length == 0 ) {
  951. textView.text = decimalString;
  952. return NO;
  953. }
  954. if((length >= 10 && !hasLeadingOne) )
  955. {
  956. if(length>15)
  957. return NO;
  958. // newString=[[newString stringByReplacingOccurrencesOfString:@"-" withString:@""] mutableCopy];
  959. // [newString insertString:@"-" atIndex:14];
  960. // textView.text = newString;
  961. // return NO;
  962. }
  963. if((length >= 11))
  964. {
  965. if(length>16)
  966. return NO;
  967. // newString=[[newString stringByReplacingOccurrencesOfString:@"-" withString:@""] mutableCopy];
  968. // [newString insertString:@"-" atIndex:16];
  969. // textView.text = newString;
  970. // return NO;
  971. }
  972. NSUInteger index = 0;
  973. NSMutableString *formattedString = [NSMutableString string];
  974. if (hasLeadingOne) {
  975. [formattedString appendString:@"1 "];
  976. index += 1;
  977. }
  978. if (length - index > 3) {
  979. NSString *areaCode = [decimalString substringWithRange:NSMakeRange(index, 3)];
  980. [formattedString appendFormat:@"(%@) ",areaCode];
  981. index += 3;
  982. }
  983. if (length - index > 3) {
  984. NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 3)];
  985. [formattedString appendFormat:@"%@ ",prefix];
  986. index += 3;
  987. }
  988. if (length - index > 4) {
  989. NSString *prefix = [decimalString substringWithRange:NSMakeRange(index, 4)];
  990. [formattedString appendFormat:@"%@-",prefix];
  991. index += 4;
  992. }
  993. NSString *remainder = [decimalString substringFromIndex:index];
  994. [formattedString appendString:remainder];
  995. textView.text = formattedString;
  996. return NO;
  997. }
  998. else if([tv_format isEqualToString:@"price"])
  999. {
  1000. // NSMutableString *newString = [[textView.text stringByReplacingCharactersInRange:range withString:text] mutableCopy];
  1001. // NSArray *components = [newString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
  1002. // NSString *decimalString = [components componentsJoinedByString:@""];
  1003. //
  1004. //
  1005. //
  1006. //
  1007. // NSString *newprice = [formatter stringFromNumber:[NSNumber numberWithInt: [decimalString intValue]]];
  1008. //
  1009. // textView.text = newprice;
  1010. NSCharacterSet *cs;
  1011. cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
  1012. //
  1013. NSString *filtered = [[text componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  1014. BOOL canChange = [text isEqualToString:filtered];
  1015. // if(canChange)
  1016. // {
  1017. //
  1018. // }
  1019. return canChange;
  1020. // return NO;
  1021. }
  1022. else
  1023. {
  1024. NSString* oldtext =textView.text;
  1025. if(oldtext==nil)
  1026. oldtext=@"";
  1027. bool canChange=true;
  1028. int max_line = [self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"lines"] intValue];
  1029. if(max_line==0)//默认只能有一行
  1030. max_line=1;
  1031. long linecount=[TextUtils countOccurencesOfString:oldtext find:@"\n"];
  1032. if(max_line!=-1) //maxline -1表示不限制行数。
  1033. {
  1034. if(linecount<=max_line-1&& [text isEqualToString:@"\n"])
  1035. {
  1036. canChange = false;
  1037. }
  1038. }
  1039. int lenth = [self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"length"] intValue];
  1040. if(lenth==0)
  1041. return canChange;
  1042. else
  1043. {
  1044. return canChange&& (lenth>textView.text.length ||[text isEqualToString:@""]);
  1045. }
  1046. }
  1047. }
  1048. //- (void)textViewDidBeginEditing:(UITextView *)textView
  1049. //{
  1050. // self.hotTextView = textView;
  1051. //}
  1052. - (void)textViewDidBeginEditing:(UITextView *)textView
  1053. {
  1054. long index = textView.tag - CONTROL_BASE;
  1055. NSString* tv_format=self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"format"];
  1056. if([tv_format isEqualToString:@"price"])
  1057. {
  1058. NSString* prefix_str= self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  1059. if(prefix_str.length==0)
  1060. prefix_str = @"";
  1061. NSString* text =[textView.text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  1062. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  1063. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  1064. NSNumber* number= [formatter numberFromString:text];
  1065. if(number!=nil)
  1066. textView.text = [NSString stringWithFormat:@"%.2f",[number doubleValue]];
  1067. }
  1068. // NSRange r = textView.selectedRange;
  1069. //
  1070. //
  1071. // textView.selectedRange = NSMakeRange(textView.text.length, 0);
  1072. // NSRange range;
  1073. //
  1074. // range.location = textView.text.length;
  1075. //
  1076. // range.length = 0;
  1077. //
  1078. // textView.selectedRange = range;
  1079. }
  1080. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
  1081. {
  1082. if (self.controlUpdateDelegate) {
  1083. [self.controlUpdateDelegate focusControlView:textView];
  1084. return NO;
  1085. }
  1086. NSLog(@"textViewShouldBeginEditing");
  1087. // long index = textView.tag - CONTROL_BASE;
  1088. //
  1089. //
  1090. // NSMutableDictionary* control_json = self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ];
  1091. //
  1092. // if([control_json[@"disable"] boolValue] )
  1093. // {
  1094. // return false;
  1095. // }
  1096. //
  1097. self.hotTextView = textView;//(UITableViewCell*)textView.superview.superview;
  1098. return textView.editable;
  1099. }
  1100. - (void)textViewDidEndEditing:(UITextView *)textView
  1101. {
  1102. long index = textView.tag - CONTROL_BASE;
  1103. //处理 prefix 和 浮点格式化
  1104. NSString* prefix_str= self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  1105. if(prefix_str.length==0)
  1106. prefix_str = @"";
  1107. NSString* text =textView.text;// [textView.text stringByReplacingOccurrencesOfString:prefix_str withString:@""];
  1108. NSString* value_type= self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value_type"];
  1109. if([value_type isEqualToString:@"float"])
  1110. text=[NSString stringWithFormat:@"%.2f",[text doubleValue] ];
  1111. // text =[NSString stringWithFormat:@"%@%@",prefix_str,text] ;
  1112. if([self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"format"] isEqualToString:@"price"])
  1113. {
  1114. text =textView.text;
  1115. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  1116. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  1117. NSNumber * nprice =[NSNumber numberWithDouble: [text doubleValue]];
  1118. if(text.length==0)
  1119. nprice=nil;
  1120. text = [formatter stringFromNumber:nprice];
  1121. text=[text stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  1122. // textView.text = newprice;
  1123. // return NO;
  1124. }
  1125. textView.text = text;
  1126. if(text==nil)
  1127. text=@"";
  1128. self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = text;
  1129. self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"dirty"] =[NSNumber numberWithBool:true];
  1130. NSDictionary* action = self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"action"];
  1131. NSArray* keys=[action allKeys];
  1132. for(int k=0;k<keys.count;k++)
  1133. {
  1134. NSMutableDictionary* act_k = action[keys[k] ];
  1135. if (![self checkActionCondition:act_k[@"condition"]])
  1136. continue;
  1137. for(int i=0;i<[act_k[@"count"] intValue];i++)
  1138. {
  1139. NSMutableDictionary * control = act_k [[NSString stringWithFormat:@"item_%d",i] ];
  1140. UITextView* tv = (UITextView*) [self findControl:control[@"control"]];
  1141. NSString* value = control[@"value"];
  1142. NSString* newvalue = nil;
  1143. if([value isEqualToString:@"*0.1"])
  1144. {
  1145. // double f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] doubleValue]*0.1;
  1146. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  1147. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  1148. NSString* prefix_str= self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  1149. if(prefix_str.length==0)
  1150. prefix_str = @"";
  1151. if(text.length==0)
  1152. {
  1153. newvalue=nil;
  1154. }
  1155. else
  1156. {
  1157. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  1158. double f = [[formatter numberFromString:text] doubleValue]*0.1;
  1159. // if(tv==self.hotTextView)
  1160. // {
  1161. //
  1162. // newvalue = [NSString stringWithFormat:@"%.2f",f];
  1163. // }
  1164. // else
  1165. {
  1166. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  1167. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  1168. // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  1169. }
  1170. }
  1171. self.page_controlTemplate [control[@"control"] ][@"value"] = newvalue;
  1172. self.page_controlTemplate [control[@"control"] ][@"dirty"] =[NSNumber numberWithBool:true];
  1173. }
  1174. else if([value isEqualToString:@"*0.9"])
  1175. {
  1176. // double f= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] doubleValue]*0.9;
  1177. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  1178. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  1179. NSString* prefix_str= self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  1180. if(prefix_str.length==0)
  1181. prefix_str = @"";
  1182. if(text.length==0)
  1183. {
  1184. newvalue=nil;
  1185. }
  1186. else
  1187. {
  1188. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  1189. double f = [[formatter numberFromString:text] doubleValue]*0.9;
  1190. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f]];
  1191. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  1192. }
  1193. // newvalue = [NSString stringWithFormat:@"$ %.2f",f];
  1194. self.page_controlTemplate [control[@"control"] ][@"value"] = newvalue;
  1195. self.page_controlTemplate [control[@"control"] ][@"dirty"] = [NSNumber numberWithBool:true];
  1196. }
  1197. else if([value isEqualToString:@"control_6-control_7"])
  1198. {
  1199. UITextView* tv6 = (UITextView*) [self findControl:@"control_10"];
  1200. // float f6= [[tv6.text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue];
  1201. //
  1202. // float f7= [[text stringByReplacingOccurrencesOfString:@"$" withString:@"" ] floatValue];
  1203. NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
  1204. formatter.numberStyle =kCFNumberFormatterCurrencyStyle;
  1205. NSString* prefix_str= self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"prefix_str"];
  1206. if(prefix_str.length==0)
  1207. prefix_str = @"";
  1208. NSString * text6=[tv6.text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  1209. text=[text stringByReplacingOccurrencesOfString:prefix_str withString:@"$"];
  1210. double f6 =0.0;
  1211. // if(tv6==self.hotTextView)
  1212. // f6=[tv6.text doubleValue];
  1213. // else
  1214. f6=[[formatter numberFromString:text6] doubleValue];
  1215. double f7 = [[formatter numberFromString:text] doubleValue];
  1216. newvalue = [formatter stringFromNumber:[NSNumber numberWithDouble: f6-f7]];
  1217. newvalue=[newvalue stringByReplacingOccurrencesOfString:@"$" withString:prefix_str];
  1218. // newvalue = [NSString stringWithFormat:@"$ %.2f",f6-f7];
  1219. self.page_controlTemplate [control[@"control"] ][@"value"] = newvalue;
  1220. self.page_controlTemplate [control[@"control"] ][@"dirty"] = [NSNumber numberWithBool:true];
  1221. }
  1222. tv.text = newvalue;
  1223. }
  1224. }
  1225. // int count = [self.controlTemplate[@"count"] intValue];
  1226. // for(int i=0;i<count;i++)
  1227. // {
  1228. // NSMutableDictionary * control =;
  1229. //[self update_newprice];
  1230. }
  1231. /*
  1232. #pragma mark - Navigation
  1233. // In a storyboard-based application, you will often want to do a little preparation before navigation
  1234. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  1235. // Get the new view controller using [segue destinationViewController].
  1236. // Pass the selected object to the new view controller.
  1237. }
  1238. */
  1239. #pragma mark signature clicked
  1240. //touchimageview Delegate
  1241. -(void) signature:(TouchImageView *)touchImageView
  1242. {
  1243. __weak __typeof(self)weakSelf = self;
  1244. long index = touchImageView.tag - CONTROL_BASE;
  1245. NSMutableDictionary* control_json = self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ];
  1246. // CGRect cellrect_screen = [RAUtils relativeFrameForScreenWithView:trigger];
  1247. //
  1248. //
  1249. // CGRect rect1=[self.view convertRect:cellrect_screen fromView:[[[UIApplication sharedApplication] delegate] window]];
  1250. //
  1251. // DebugLog(@"convert1 %@",NSStringFromCGRect(rect1));
  1252. SignatureListViewController *signatureVC = [ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureListViewController"];
  1253. signatureVC.title=@"Signatures";
  1254. signatureVC.signatureData = self.signatureData;
  1255. signatureVC.subType = control_json[@"name"];
  1256. signatureVC.can_fill= [control_json[@"can_fill"] boolValue];
  1257. signatureVC.signature_ratio= touchImageView.frame.size;
  1258. // signatureVC.imageView = touchImageView;
  1259. signatureVC.blk_Select =^(NSString* file)
  1260. {
  1261. // NSString* file_name=[file lastPathComponent];
  1262. NSData* img_data=[ImageUtils load_img:file];
  1263. if(img_data!=nil)
  1264. {
  1265. UIImage* image=[UIImage imageWithData:img_data];
  1266. touchImageView.image = image;
  1267. long index = touchImageView.tag - CONTROL_BASE;
  1268. weakSelf.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  1269. weakSelf.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"dirty"] = [NSNumber numberWithBool:true];
  1270. }
  1271. };
  1272. signatureVC.blk_Fill =^(NSString* file,bool fillFullDoc)
  1273. {
  1274. if(weakSelf.blk_Fill)
  1275. weakSelf.blk_Fill(file,control_json[@"name"],fillFullDoc);
  1276. // for(int j=0;j<[self.controlTemplate[@"count"] intValue];j++)
  1277. // {
  1278. // NSMutableDictionary* dest =self.controlTemplate[[NSString stringWithFormat:@"control_%d",j]];
  1279. // NSString* type = dest[@"type"];
  1280. // if(![type isEqualToString:@"Signature"])
  1281. // continue;
  1282. // if([dest[@"name"] isEqualToString:control_json[@"name"]])
  1283. // {
  1284. // dest[@"value"]=file;
  1285. // }
  1286. // }
  1287. [weakSelf refreshControl];
  1288. // // NSString* file_name=[file lastPathComponent];
  1289. //
  1290. // NSData* img_data=[ImageUtils load_img:file];
  1291. //
  1292. // if(img_data!=nil)
  1293. // {
  1294. //
  1295. // UIImage* image=[UIImage imageWithData:img_data];
  1296. // touchImageView.image = image;
  1297. //
  1298. // long index = touchImageView.tag - CONTROL_BASE;
  1299. // self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  1300. //
  1301. // }
  1302. };
  1303. signatureVC.blk_Add = ^()
  1304. {
  1305. __block UIImage* signimg=nil;
  1306. SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"];
  1307. vc.title =[NSString stringWithFormat:@"Signature: %@",control_json[@"name"]];
  1308. vc.signature_ratio= touchImageView.frame.size;
  1309. vc.linewidth = [control_json[@"linewidth"] intValue];
  1310. vc.onReturnImg = ^(UIImage* img)
  1311. {
  1312. signimg = img;
  1313. if(signimg!=nil)
  1314. {
  1315. NSString* file=[PDFUtils addSignature:signimg to:self.signatureData subType:control_json[@"name"]];
  1316. long index = touchImageView.tag - CONTROL_BASE;
  1317. self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  1318. self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"dirty"] =[NSNumber numberWithBool:true];
  1319. touchImageView.image = signimg;
  1320. }
  1321. };
  1322. // orderinfoVC.url_type = URL_REMOTE;
  1323. // orderinfoVC.request_url=URL_CARTDELIVERY;
  1324. //
  1325. // orderinfoVC.params = params;
  1326. //
  1327. // orderinfoVC.delegate=self;
  1328. //
  1329. // if(checked.count==count)
  1330. // {
  1331. // orderinfoVC.have_tail = true
  1332. // }
  1333. [self.navigationController pushViewController:vc animated:true];
  1334. };
  1335. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:signatureVC];
  1336. navi.modalPresentationStyle=UIModalPresentationPopover;
  1337. UIPopoverPresentationController* popPc = navi.popoverPresentationController;
  1338. popPc.permittedArrowDirections = UIPopoverArrowDirectionAny;
  1339. popPc.sourceView = touchImageView;
  1340. popPc.delegate = nil;
  1341. [self presentViewController:navi animated:true completion:nil];
  1342. }
  1343. - (void) TouchImageViewOnLongPress:(TouchImageView *)touchImageView
  1344. {
  1345. long index = touchImageView.tag - CONTROL_BASE;
  1346. NSMutableDictionary* control_json = self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ];
  1347. if([control_json[@"disable"] boolValue] )
  1348. {
  1349. return;
  1350. }
  1351. if(touchImageView.image!=nil)
  1352. {
  1353. NSLog(@"long press");
  1354. [self becomeFirstResponder];
  1355. UIView* v = touchImageView;
  1356. self.tiv_index = v.tag;
  1357. UIMenuItem *clear = [[UIMenuItem alloc] initWithTitle:@"Clear" action:@selector(touchImageClear:)];
  1358. // UIMenuItem *modify = [[UIMenuItem alloc] initWithTitle:@"Modify" action:@selector(touchImageModify:)];
  1359. // [detail setValue:_id forKey:@"_id"];
  1360. UIMenuController *menu = [UIMenuController sharedMenuController];
  1361. [menu setMenuItems:[NSArray arrayWithObjects:clear,/*modify,*/ nil]];
  1362. [menu setTargetRect:v.frame inView:v.superview];
  1363. [menu setMenuVisible:YES animated:YES];
  1364. }
  1365. }
  1366. //- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  1367. //{
  1368. // if (action == @selector(showDetail:))
  1369. // return YES;//显示
  1370. // else
  1371. // return NO;//不显示
  1372. //}
  1373. - (void) touchImageClear:(id) sender
  1374. {
  1375. long index = self.tiv_index - CONTROL_BASE;
  1376. for(UIView*v in self.editControlView.subviews)
  1377. {
  1378. if(v.tag==self.tiv_index)
  1379. ((UIImageView*)v).image = nil;
  1380. }
  1381. // NSMutableDictionary* control_json = self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ];
  1382. self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = @"";
  1383. self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"dirty"] =[NSNumber numberWithBool:true];
  1384. }
  1385. - (void) touchImageModify:(id) sender
  1386. {
  1387. }
  1388. - (void)TouchImageViewOnTouche:(TouchImageView *)touchImageView
  1389. {
  1390. if (self.controlUpdateDelegate) {
  1391. [self.controlUpdateDelegate focusControlView:touchImageView];
  1392. return;
  1393. }
  1394. NSLog(@"signature button clicked;");
  1395. //
  1396. // __weak __typeof(self)weakSelf = self;
  1397. //
  1398. //
  1399. //
  1400. long index = touchImageView.tag - CONTROL_BASE;
  1401. NSMutableDictionary* control_json = self.page_controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ];
  1402. if([control_json[@"disable"] boolValue] )
  1403. {
  1404. return;
  1405. }
  1406. bool bvalidation=[control_json[@"required_validation"] boolValue];
  1407. NSString* pwd= control_json[@"validation_code"];
  1408. if(bvalidation&& pwd.length>0)
  1409. {
  1410. //UIAlertControllerStyle两种类型UIAlertControllerStyleAlert类似UIAlertView
  1411. //UIAlertControllerStyleActionSheet类似UIActionSheet
  1412. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Authorization Verification" message:@"Please enter authorization code." preferredStyle:UIAlertControllerStyleAlert];
  1413. //block代码块取代了delegate
  1414. [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  1415. }];
  1416. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  1417. UITextField *name = alertControl.textFields.firstObject;
  1418. if(![name.text isEqualToString:pwd])
  1419. {
  1420. [RAUtils message_alert:@"You are not authorized to change this field" title:@"Authorization verification failed." controller:self];
  1421. return;
  1422. }
  1423. else
  1424. {
  1425. [self signature:touchImageView];
  1426. }
  1427. }];
  1428. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  1429. DebugLog(@"Cancel");
  1430. return;
  1431. }];
  1432. [alertControl addAction:actionOne];
  1433. [alertControl addAction:alertthree];
  1434. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  1435. [self presentViewController:alertControl animated:YES completion:nil];
  1436. }
  1437. else
  1438. {
  1439. [self signature:touchImageView];
  1440. }
  1441. //// menu.selector = self.selector;
  1442. ////
  1443. //// menu.selectordelegate = self;
  1444. //
  1445. // // 1.创建一个UIPopover
  1446. // UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:[[UINavigationController alloc] initWithRootViewController:menu]];
  1447. //
  1448. //
  1449. //
  1450. // UIPopoverPresentationController
  1451. // // 2.设置尺寸
  1452. // // popover.popoverContentSize = CGSizeMake(320, 44 * 5);
  1453. //
  1454. // // 3.从哪里显示出来 --> 指向item
  1455. //
  1456. // [popover presentPopoverFromRect:self.view.frame inView:self.view permittedArrowDirections:0 animated:YES];
  1457. // // [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  1458. //
  1459. //// self.popover = popover;
  1460. // popover pop
  1461. //
  1462. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  1463. // __block int tag = touchImageView.tag;
  1464. // UIViewController* vc=[RAUtils getViewController :touchImageView];
  1465. //
  1466. // if(self.editable==true)
  1467. // {
  1468. //
  1469. //
  1470. // ImageUploadViewController * uploadvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"ImageUploadViewController"];
  1471. //
  1472. // // UIImage* img =[self.buttonImg backgroundImageForState:UIControlStateNormal];;
  1473. //
  1474. // if(self.img_validate)
  1475. // uploadvc.img= touchImageView.image;
  1476. //
  1477. // uploadvc.returnValue = ^(NSString* url_down,NSString* url_up,UIImage* img)
  1478. // {
  1479. //
  1480. // self.imgs[tag] = url_up;
  1481. //
  1482. // NSString* newurl=[RAUtils arr2string:self.imgs separator:@"," trim:false];
  1483. //
  1484. // touchImageView.image=img;
  1485. //
  1486. // if(self.imgChanged)
  1487. // self.imgChanged(url_down,newurl,tag,url_up);
  1488. //
  1489. // };
  1490. //
  1491. // [vc.navigationController pushViewController:uploadvc animated:false];
  1492. // }
  1493. // else
  1494. // {
  1495. // if(touchImageView.image==nil)
  1496. // return ;
  1497. // ImageViewController * imagevc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"ImageViewController"];
  1498. //
  1499. //
  1500. // UIImage* img=touchImageView.image;
  1501. //
  1502. // if(self.img_validate)
  1503. // imagevc.image = img;//.imageView.image = [self.buttonImg backgroundImageForState:UIControlStateNormal];
  1504. //
  1505. // // uploadvc.returnValue = ^(NSString* url_down,NSString* url_up,UIImage* img)
  1506. // // {
  1507. // //
  1508. // // [self.buttonImg setBackgroundImage:img forState:UIControlStateNormal];
  1509. // //
  1510. // // if(self.imgChanged)
  1511. // // self.imgChanged(url_down,url_up);
  1512. // //
  1513. // // };
  1514. //
  1515. // [vc.navigationController pushViewController:imagevc animated:false];
  1516. // }
  1517. // // bundleVC.content_data = self.bundle_item;
  1518. //
  1519. }
  1520. #pragma mark Responding to keyboard events
  1521. - (CGRect)relativeFrameForScreenWithView:(UIView *)v
  1522. {
  1523. UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
  1524. CGRect rect=[v convertRect: v.bounds toView:window];
  1525. return rect;
  1526. // BOOL iOS7 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 7;
  1527. //
  1528. // CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
  1529. // if (!iOS7) {
  1530. // screenHeight -= 20;
  1531. // }
  1532. // UIView *view = v;
  1533. // CGFloat x = .0;
  1534. // CGFloat y = .0;
  1535. // while (view.frame.size.width != 320 || view.frame.size.height != screenHeight) {
  1536. // x += view.frame.origin.x;
  1537. // y += view.frame.origin.y;
  1538. // view = view.superview;
  1539. // if ([view isKindOfClass:[UIScrollView class]]) {
  1540. // x -= ((UIScrollView *) view).contentOffset.x;
  1541. // y -= ((UIScrollView *) view).contentOffset.y;
  1542. // }
  1543. // }
  1544. // return CGRectMake(x, y, v.frame.size.width, v.frame.size.height);
  1545. }
  1546. - (void)keyboardWillChangeFrame:(NSNotification *)notification {
  1547. NSLog(@"keyboardWillChangeFrame");
  1548. }
  1549. - (void)keyboardWillShow:(NSNotification *)notification {
  1550. NSLog(@"keyboardWillShow");
  1551. if(self.keyboard_show)
  1552. return;
  1553. self.keyboard_show=true;
  1554. /*
  1555. Reduce the size of the text view so that it's not obscured by the keyboard.
  1556. Animate the resize so that it's in sync with the appearance of the keyboard.
  1557. */
  1558. NSDictionary *userInfo = [notification userInfo];
  1559. NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  1560. CGSize keyboardSize = [aValue CGRectValue].size;
  1561. NSLog(@"keyboard height:%f",keyboardSize.height);
  1562. CGRect cellrect_screen = [self relativeFrameForScreenWithView:self.hotTextView];
  1563. CGRect rect_screen = [ UIScreen mainScreen ].bounds;
  1564. int cellpos = cellrect_screen.origin.y+cellrect_screen.size.height;
  1565. if(cellpos>rect_screen.size.height-keyboardSize.height)
  1566. {
  1567. // self.resize = true;
  1568. self.keyboard_h =keyboardSize.height;
  1569. CGPoint contentOffsetPoint = self.pdfScrollView.contentOffset;
  1570. CGSize contentSize =self.pdfScrollView.contentSize;//frame.size;
  1571. CGSize size1 = self.pdfScrollView.bounds.size;
  1572. NSLog(@"frame:%@ bound:%@", NSStringFromCGSize(contentSize),NSStringFromCGSize(size1) );
  1573. contentSize.height+=self.keyboard_h;
  1574. contentOffsetPoint.y+=self.keyboard_h;
  1575. self.pdfScrollView.contentSize =contentSize;
  1576. self.pdfScrollView.contentOffset=contentOffsetPoint;
  1577. }
  1578. // self.pdfScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.keyboard_h);
  1579. // CGSize tablecontent =self.editorTable.contentSize;
  1580. // tablecontent.height=tablecontent.height+self.keyboard_h;
  1581. // self.editorTable.contentSize=tablecontent;
  1582. // }
  1583. // Animate the resize of the text view's frame in sync with the keyboard's appearance.
  1584. // [self moveInputBarWithKeyboardHeight:keyboardRect.size.height withDuration:animationDuration];
  1585. }
  1586. - (void)keyboardWillHide:(NSNotification *)notification {
  1587. NSLog(@"keyboardWillHide");
  1588. self.keyboard_show=false;
  1589. // NSDictionary* userInfo = [notification userInfo];
  1590. /*
  1591. Restore the size of the text view (fill self's view).
  1592. Animate the resize so that it's in sync with the disappearance of the keyboard.
  1593. */
  1594. // NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
  1595. // NSTimeInterval animationDuration;
  1596. // [animationDurationValue getValue:&animationDuration];
  1597. CGPoint contentOffsetPoint = self.pdfScrollView.contentOffset;
  1598. CGSize contentSize =self.pdfScrollView.contentSize;//frame.size;
  1599. contentSize.height-=self.keyboard_h;
  1600. contentOffsetPoint.y-=self.keyboard_h;
  1601. self.pdfScrollView.contentSize=contentSize;
  1602. self.pdfScrollView.contentOffset=contentOffsetPoint;
  1603. // CGSize tablecontent =self.pdfScrollView.contentSize;
  1604. // tablecontent.height=tablecontent.height-self.keyboard_h;
  1605. // self.editorTable.contentSize=tablecontent;
  1606. // self.pdfScrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
  1607. self.keyboard_h= 0;
  1608. // NSLog(@"before refresh %@",NSStringFromCGSize(self.editorTable.contentSize));
  1609. // if(self.resize)
  1610. // {
  1611. // NSTimeInterval animationDuration = 0.30f;
  1612. // CGRect frame = self.view.frame;
  1613. // // if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
  1614. // // { //还原界面
  1615. // // moveY = prewMoveY;
  1616. // frame.origin.y +=self.ioffset;
  1617. // frame.size. height -=self.ioffset;
  1618. // self.view.frame = frame;
  1619. // // }
  1620. // //self.view移回原位置
  1621. // [UIView beginAnimations:@"ResizeView" context:nil];
  1622. // [UIView setAnimationDuration:animationDuration];
  1623. // self.view.frame = frame;
  1624. // [UIView commitAnimations];
  1625. // //[textField resignFirstResponder];
  1626. // self.ioffset=0;
  1627. // }
  1628. // [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];
  1629. }
  1630. #pragma mark - Update Control
  1631. - (void)tapRecognizer:(UITapGestureRecognizer *)recognizer {
  1632. UIView *v = recognizer.view;
  1633. if (self.controlUpdateDelegate) {
  1634. [self.controlUpdateDelegate focusControlView:v];
  1635. }
  1636. }
  1637. - (NSDictionary *)updatePageControl {
  1638. int count = [self.page_controlTemplate[@"count"] intValue];
  1639. for(int i=0;i<count;i++)
  1640. {
  1641. NSMutableDictionary * control = [self.page_controlTemplate [[NSString stringWithFormat:@"control_%d",i]] mutableCopy];
  1642. if (control == nil) {
  1643. continue;
  1644. }
  1645. int tag = i + CONTROL_BASE;
  1646. UIView *v = [self.editControlView viewWithTag:tag];
  1647. if (v == nil) {
  1648. continue;
  1649. }
  1650. CGRect frame = [self scaleControl:v.frame from:self.editControlView.bounds.size to:CGSizeMake(768, 960.5)];
  1651. int x = frame.origin.x;
  1652. int y = frame.origin.y;
  1653. int w = frame.size.width;
  1654. int h = frame.size.height;
  1655. [control setValue:@(x) forKey:@"pos_x"];
  1656. [control setValue:@(y) forKey:@"pos_y"];
  1657. [control setValue:@(w) forKey:@"width"];
  1658. [control setValue:@(h) forKey:@"height"];
  1659. if([control[@"type"] isEqualToString:@"TextView"])
  1660. {
  1661. }
  1662. else if([control[@"type"] isEqualToString:@"Button"])
  1663. {
  1664. }
  1665. else if([control[@"type"] isEqualToString:@"Check"])
  1666. {
  1667. NSMutableArray* cadedate = [control[@"cadedate"] mutableCopy];
  1668. for(int k = 0; k < cadedate.count; k++)
  1669. {
  1670. NSMutableArray* item = [cadedate[k] mutableCopy];
  1671. NSMutableArray* checkmarker = [item[1] mutableCopy];
  1672. if(checkmarker.count==0)
  1673. {
  1674. continue;
  1675. } else {
  1676. UIView *marker = [self.editControlView viewWithTag:CHECK_BASE + i * 1000 + k];
  1677. if (marker == nil) {
  1678. continue;
  1679. }
  1680. CGRect marker_frame = [self scaleControl:marker.frame from:self.editControlView.bounds.size to:CGSizeMake(768, 960.5)];
  1681. int marker_x = marker_frame.origin.x;
  1682. int marker_y = marker_frame.origin.y;
  1683. int marker_w = marker_frame.size.width;
  1684. int marker_h = marker_frame.size.height;
  1685. [checkmarker replaceObjectAtIndex:0 withObject:@(marker_x)];
  1686. [checkmarker replaceObjectAtIndex:1 withObject:@(marker_y)];
  1687. [item replaceObjectAtIndex:1 withObject:checkmarker];
  1688. [cadedate replaceObjectAtIndex:k withObject:item];
  1689. }
  1690. }
  1691. [control setObject:cadedate forKey:@"cadedate"];
  1692. }
  1693. else if([control[@"type"] isEqualToString:@"Signature"])
  1694. {
  1695. }
  1696. else if([control[@"type"] isEqualToString:@"Image"])
  1697. {
  1698. }
  1699. else if([control[@"type"] isEqualToString:@"Label"])
  1700. {
  1701. }
  1702. else if([control[@"type"] isEqualToString:@"DatePicker"])
  1703. {
  1704. }
  1705. [self.page_controlTemplate setObject:control forKey:[NSString stringWithFormat:@"control_%d",i]];
  1706. }
  1707. return self.page_controlTemplate;
  1708. }
  1709. @end