PageViewController.m 76 KB

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