PageViewController.m 90 KB

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