StampTextViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // StampTextViewController.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "StampTextViewController.h"
  11. #import "StampTextCell.h"
  12. @interface StampTextViewController () <UITableViewDataSource,UITableViewDelegate,StampTextCellDelegate> {
  13. UITableView *_tableView;
  14. NSArray *_CustomString;
  15. NSArray *_CustomType;
  16. //自定义text stamp的四个属性
  17. NSString *_textStampText;
  18. NSInteger _textStampStyle;
  19. BOOL _textStampHaveDate;
  20. BOOL _textStampHaveTime;
  21. }
  22. @property (nonatomic,assign) BOOL isWillAppear;
  23. @property (nonatomic,retain) UIBarButtonItem *rightItem;
  24. @property (nonatomic,retain) StampTextCell *previewCell;
  25. @property (nonatomic,retain) UITextField * textTextFile;
  26. @end
  27. @implementation StampTextViewController
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. //title
  32. self.title = NSLocalizedString(@"New Text Stamp", nil);
  33. [self.navigationController.navigationBar setTranslucent:NO];
  34. [self initData];
  35. [self.view setBackgroundColor:[UIColor whiteColor]];
  36. [self initNavigationItem];
  37. //_setView
  38. [self initTableView];
  39. [self.view addSubview:_tableView];
  40. }
  41. - (void)viewWillAppear:(BOOL)animated
  42. {
  43. [super viewWillAppear:animated];
  44. [self updateCurInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation];
  45. }
  46. - (void)viewDidAppear:(BOOL)animated
  47. {
  48. [super viewDidAppear:animated];
  49. [self.textTextFile becomeFirstResponder];
  50. }
  51. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  52. {
  53. [self updateCurInterfaceOrientation:toInterfaceOrientation];
  54. }
  55. #pragma mark - Rotation method
  56. - (void)updateCurInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  57. {
  58. if (_tableView) {
  59. [_tableView layoutIfNeeded];
  60. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
  61. StampTextCell *cell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath];
  62. cell.stampTextField.text = _textStampText;
  63. indexPath = [NSIndexPath indexPathForRow:1 inSection:1];
  64. cell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath];
  65. [cell selectColor:_textStampStyle];
  66. indexPath = [NSIndexPath indexPathForRow:2 inSection:1];
  67. cell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath];
  68. [cell.haveDateSwitch setOn:_textStampHaveDate];
  69. [self switchValueChanged_Date:cell.haveDateSwitch];
  70. indexPath = [NSIndexPath indexPathForRow:3 inSection:1];
  71. cell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath];
  72. [cell.haveTimeSwitch setOn:_textStampHaveTime];
  73. [self switchValueChanged_Time:cell.haveTimeSwitch];
  74. [_tableView reloadData];
  75. }
  76. }
  77. - (void)dealloc {
  78. Block_release(_callback);
  79. [_CustomString release];
  80. if (_tableView) {
  81. _tableView.dataSource = nil;
  82. _tableView.delegate = nil;
  83. [_tableView removeFromSuperview];
  84. [_tableView release];
  85. }
  86. [_CustomType release];
  87. [_textStampText release];
  88. [_previewCell release];
  89. [_textTextFile release];
  90. [super dealloc];
  91. }
  92. #pragma mark - Init Data
  93. - (void)initData
  94. {
  95. _textStampText = @"";
  96. [self initCustomStrings];
  97. [self initCustomType];
  98. }
  99. - (void)initCustomStrings
  100. {
  101. if (!_CustomString)
  102. {
  103. _CustomString = [[NSArray alloc] initWithObjects:
  104. NSLocalizedString(@"Text", nil), NSLocalizedString(@"Color", nil),
  105. NSLocalizedString(@"Date", nil), NSLocalizedString(@"Time", nil), nil];
  106. }
  107. }
  108. - (void)initCustomType
  109. {
  110. if (!_CustomType)
  111. {
  112. _CustomType = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil];
  113. }
  114. }
  115. #pragma mark - UI Init
  116. - (void)initNavigationItem
  117. {
  118. //rigth
  119. UIBarButtonItem *tRigthItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
  120. target:self
  121. action:@selector(buttonItemClicked_Done:)];
  122. self.rightItem = tRigthItem;
  123. [tRigthItem release];
  124. }
  125. - (void)initTableView
  126. {
  127. if (!_tableView)
  128. {
  129. _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
  130. _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
  131. _tableView.dataSource = self;
  132. _tableView.delegate = self;
  133. }
  134. }
  135. #pragma mark - UITableViewDataSource & UITableViewDelegate
  136. // 设置表格元素中有几个子元素
  137. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  138. {
  139. return 2;
  140. }
  141. // 设置表格中元素的个数
  142. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  143. {
  144. switch (section)
  145. {
  146. case 0:
  147. return 1;
  148. break;
  149. case 1:
  150. return 4;
  151. default:
  152. return 0;
  153. break;
  154. }
  155. }
  156. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  157. {
  158. switch (section)
  159. {
  160. case 0:
  161. return NSLocalizedString(@"Preview", nil);
  162. break;
  163. case 1:
  164. return nil;
  165. default:
  166. return nil;
  167. break;
  168. }
  169. }
  170. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  171. {
  172. switch (indexPath.section)
  173. {
  174. case 0:
  175. return 60;
  176. break;
  177. case 1:
  178. {
  179. if (1 == indexPath.row) {
  180. return 150;
  181. } else {
  182. return 50;
  183. }
  184. }
  185. break;
  186. default:
  187. return 50;
  188. break;
  189. }
  190. }
  191. // 设置表格每个元素的显示样式
  192. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  193. {
  194. static NSString *tCellIdentifier = @"CustomStampContent";
  195. StampTextCell *tCell = [tableView dequeueReusableCellWithIdentifier:tCellIdentifier];
  196. if (tCell == nil) {
  197. tCell = [[[StampTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tCellIdentifier] autorelease];
  198. tCell.delegate = self;
  199. }
  200. switch (indexPath.section)
  201. {
  202. case 0:
  203. {
  204. [tCell setCellStyle:CustomTextCellType_Preview label:_textStampText];
  205. tCell.preView.textStampText = _textStampText;
  206. [tCell.preView setTextStampStyle:_textStampStyle];
  207. tCell.preView.textStampHaveDate = _textStampHaveDate;
  208. tCell.preView.textStampHaveTime = _textStampHaveTime;
  209. [tCell preViewReDisplay];
  210. self.previewCell = tCell;
  211. break;
  212. }
  213. case 1:
  214. {
  215. int tType = [[_CustomType objectAtIndex:indexPath.row] intValue];
  216. [tCell setCellStyle:(CustomTextCellType)tType label:[_CustomString objectAtIndex:indexPath.row]];
  217. switch (indexPath.row)
  218. {
  219. case 0:
  220. tCell.stampTextField.text = _textStampText;
  221. self.textTextFile = tCell.stampTextField;
  222. break;
  223. case 1:
  224. [tCell selectColor:_textStampStyle];
  225. break;
  226. case 2:
  227. [tCell.haveDateSwitch setOn:_textStampHaveDate];
  228. [self switchValueChanged_Date:tCell.haveDateSwitch];
  229. break;
  230. case 3:
  231. [tCell.haveTimeSwitch setOn:_textStampHaveTime];
  232. [self switchValueChanged_Time:tCell.haveTimeSwitch];
  233. break;
  234. default:
  235. break;
  236. }
  237. break;
  238. }
  239. default:
  240. break;
  241. }
  242. return tCell;
  243. }
  244. #pragma mark - Button Action
  245. - (void)buttonItemClicked_Back:(id)sender
  246. {
  247. [self.navigationController popViewControllerAnimated:YES];
  248. }
  249. - (void)buttonItemClicked_Done:(id)sender
  250. {
  251. //保存并退出
  252. //文本为空暂不保存
  253. if (![_textStampText isEqualToString:@""] || _textStampHaveDate || _textStampHaveTime)
  254. {
  255. NSMutableDictionary *tStampItem = [[NSMutableDictionary alloc] init];
  256. if (_textStampText.length > 0) {
  257. [tStampItem setObject:_textStampText forKey:@"text"];
  258. [tStampItem setObject:[NSNumber numberWithInteger:_textStampStyle] forKey:@"style"];
  259. [tStampItem setObject:[NSNumber numberWithBool:_textStampHaveDate] forKey:@"haveDate"];
  260. [tStampItem setObject:[NSNumber numberWithBool:_textStampHaveTime] forKey:@"haveTime"];
  261. } else {
  262. [tStampItem setObject:self.previewCell.preView.dateTime forKey:@"text"];
  263. [tStampItem setObject:[NSNumber numberWithInteger:_textStampStyle] forKey:@"style"];
  264. [tStampItem setObject:[NSNumber numberWithBool:NO] forKey:@"haveDate"];
  265. [tStampItem setObject:[NSNumber numberWithBool:NO] forKey:@"haveTime"];
  266. }
  267. if (self.callback) {
  268. self.callback(tStampItem);
  269. self.callback = nil;
  270. }
  271. [tStampItem release];
  272. }
  273. [self.navigationController popToRootViewControllerAnimated:YES];
  274. }
  275. #pragma mark - TStampTextCellDelegate
  276. - (void)switchValueChanged_Date:(id)sender
  277. {
  278. UISwitch *tSwitch = (UISwitch *)sender;
  279. _textStampHaveDate = [tSwitch isOn];
  280. self.previewCell.preView.textStampHaveDate = _textStampHaveDate;
  281. [self.previewCell preViewReDisplay];
  282. if (_textStampHaveDate || _textStampHaveTime) {
  283. self.navigationItem.rightBarButtonItem = self.rightItem;
  284. } else {
  285. self.navigationItem.rightBarButtonItem = [_textStampText isEqualToString:@""]? nil:self.rightItem;
  286. }
  287. }
  288. - (void)switchValueChanged_Time:(id)sender
  289. {
  290. UISwitch *tSwitch = (UISwitch *)sender;
  291. _textStampHaveTime = [tSwitch isOn];
  292. self.previewCell.preView.textStampHaveTime = _textStampHaveTime;
  293. [self.previewCell preViewReDisplay];
  294. if (_textStampHaveDate || _textStampHaveTime) {
  295. self.navigationItem.rightBarButtonItem = self.rightItem;
  296. } else {
  297. self.navigationItem.rightBarButtonItem = [_textStampText isEqualToString:@""]? nil:self.rightItem;
  298. }
  299. }
  300. - (void)textFieldWithText_Text:(id)sender
  301. {
  302. UITextField *textField = (UITextField *)sender;
  303. if (!_textStampText) {
  304. [_textStampText release];
  305. _textStampText = nil;
  306. }
  307. _textStampText = [textField.text copy];
  308. if (_textStampHaveDate || _textStampHaveTime) {
  309. self.navigationItem.rightBarButtonItem = self.rightItem;
  310. } else {
  311. self.navigationItem.rightBarButtonItem = [_textStampText isEqualToString:@""]? nil:self.rightItem;
  312. }
  313. //刷新preview
  314. NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
  315. StampTextCell *tCell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath];
  316. tCell.preView.textStampText = _textStampText;
  317. [tCell preViewReDisplay];
  318. }
  319. - (void)buttonItemClicked_Color:(id)sender
  320. {
  321. UIButton *tBtn = (UIButton *)sender;
  322. _textStampStyle = tBtn.tag;
  323. NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
  324. StampTextCell *tCell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath];
  325. [tCell.preView setTextStampStyle:_textStampStyle];
  326. [tCell preViewReDisplay];
  327. }
  328. @end