PageViewController.m 84 KB

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