// // StampTextViewController.m // PDFReader // // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved. // // The PDF Reader Sample applications are licensed with a modified BSD license. // Please see License for details. This notice may not be removed from this file. // #import "StampTextViewController.h" #import "StampTextCell.h" @interface StampTextViewController () { UITableView *_tableView; NSArray *_CustomString; NSArray *_CustomType; //自定义text stamp的四个属性 NSString *_textStampText; NSInteger _textStampStyle; BOOL _textStampHaveDate; BOOL _textStampHaveTime; } @property (nonatomic,assign) BOOL isWillAppear; @property (nonatomic,retain) UIBarButtonItem *rightItem; @property (nonatomic,retain) StampTextCell *previewCell; @property (nonatomic,retain) UITextField * textTextFile; @end @implementation StampTextViewController - (void)viewDidLoad { [super viewDidLoad]; //title self.title = NSLocalizedString(@"New Text Stamp", nil); [self.navigationController.navigationBar setTranslucent:NO]; [self initData]; [self.view setBackgroundColor:[UIColor whiteColor]]; [self initNavigationItem]; //_setView [self initTableView]; [self.view addSubview:_tableView]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self updateCurInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.textTextFile becomeFirstResponder]; } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [self updateCurInterfaceOrientation:toInterfaceOrientation]; } #pragma mark - Rotation method - (void)updateCurInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (_tableView) { [_tableView layoutIfNeeded]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; StampTextCell *cell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath]; cell.stampTextField.text = _textStampText; indexPath = [NSIndexPath indexPathForRow:1 inSection:1]; cell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath]; [cell selectColor:_textStampStyle]; indexPath = [NSIndexPath indexPathForRow:2 inSection:1]; cell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath]; [cell.haveDateSwitch setOn:_textStampHaveDate]; [self switchValueChanged_Date:cell.haveDateSwitch]; indexPath = [NSIndexPath indexPathForRow:3 inSection:1]; cell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath]; [cell.haveTimeSwitch setOn:_textStampHaveTime]; [self switchValueChanged_Time:cell.haveTimeSwitch]; [_tableView reloadData]; } } - (void)dealloc { Block_release(_callback); [_CustomString release]; if (_tableView) { _tableView.dataSource = nil; _tableView.delegate = nil; [_tableView removeFromSuperview]; [_tableView release]; } [_CustomType release]; [_textStampText release]; [_previewCell release]; [_textTextFile release]; [super dealloc]; } #pragma mark - Init Data - (void)initData { _textStampText = @""; [self initCustomStrings]; [self initCustomType]; } - (void)initCustomStrings { if (!_CustomString) { _CustomString = [[NSArray alloc] initWithObjects: NSLocalizedString(@"Text", nil), NSLocalizedString(@"Color", nil), NSLocalizedString(@"Date", nil), NSLocalizedString(@"Time", nil), nil]; } } - (void)initCustomType { if (!_CustomType) { _CustomType = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:0], [NSNumber numberWithInt:1],[NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil]; } } #pragma mark - UI Init - (void)initNavigationItem { //rigth UIBarButtonItem *tRigthItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonItemClicked_Done:)]; self.rightItem = tRigthItem; [tRigthItem release]; } - (void)initTableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight; _tableView.dataSource = self; _tableView.delegate = self; } } #pragma mark - UITableViewDataSource & UITableViewDelegate // 设置表格元素中有几个子元素 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } // 设置表格中元素的个数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch (section) { case 0: return 1; break; case 1: return 4; default: return 0; break; } } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { switch (section) { case 0: return NSLocalizedString(@"Preview", nil); break; case 1: return nil; default: return nil; break; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0: return 60; break; case 1: { if (1 == indexPath.row) { return 150; } else { return 50; } } break; default: return 50; break; } } // 设置表格每个元素的显示样式 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *tCellIdentifier = @"CustomStampContent"; StampTextCell *tCell = [tableView dequeueReusableCellWithIdentifier:tCellIdentifier]; if (tCell == nil) { tCell = [[[StampTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tCellIdentifier] autorelease]; tCell.delegate = self; } switch (indexPath.section) { case 0: { [tCell setCellStyle:CustomTextCellType_Preview label:_textStampText]; tCell.preView.textStampText = _textStampText; [tCell.preView setTextStampStyle:_textStampStyle]; tCell.preView.textStampHaveDate = _textStampHaveDate; tCell.preView.textStampHaveTime = _textStampHaveTime; [tCell preViewReDisplay]; self.previewCell = tCell; break; } case 1: { int tType = [[_CustomType objectAtIndex:indexPath.row] intValue]; [tCell setCellStyle:(CustomTextCellType)tType label:[_CustomString objectAtIndex:indexPath.row]]; switch (indexPath.row) { case 0: tCell.stampTextField.text = _textStampText; self.textTextFile = tCell.stampTextField; break; case 1: [tCell selectColor:_textStampStyle]; break; case 2: [tCell.haveDateSwitch setOn:_textStampHaveDate]; [self switchValueChanged_Date:tCell.haveDateSwitch]; break; case 3: [tCell.haveTimeSwitch setOn:_textStampHaveTime]; [self switchValueChanged_Time:tCell.haveTimeSwitch]; break; default: break; } break; } default: break; } return tCell; } #pragma mark - Button Action - (void)buttonItemClicked_Back:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } - (void)buttonItemClicked_Done:(id)sender { //保存并退出 //文本为空暂不保存 if (![_textStampText isEqualToString:@""] || _textStampHaveDate || _textStampHaveTime) { NSMutableDictionary *tStampItem = [[NSMutableDictionary alloc] init]; if (_textStampText.length > 0) { [tStampItem setObject:_textStampText forKey:@"text"]; [tStampItem setObject:[NSNumber numberWithInteger:_textStampStyle] forKey:@"style"]; [tStampItem setObject:[NSNumber numberWithBool:_textStampHaveDate] forKey:@"haveDate"]; [tStampItem setObject:[NSNumber numberWithBool:_textStampHaveTime] forKey:@"haveTime"]; } else { [tStampItem setObject:self.previewCell.preView.dateTime forKey:@"text"]; [tStampItem setObject:[NSNumber numberWithInteger:_textStampStyle] forKey:@"style"]; [tStampItem setObject:[NSNumber numberWithBool:NO] forKey:@"haveDate"]; [tStampItem setObject:[NSNumber numberWithBool:NO] forKey:@"haveTime"]; } if (self.callback) { self.callback(tStampItem); self.callback = nil; } [tStampItem release]; } [self.navigationController popToRootViewControllerAnimated:YES]; } #pragma mark - TStampTextCellDelegate - (void)switchValueChanged_Date:(id)sender { UISwitch *tSwitch = (UISwitch *)sender; _textStampHaveDate = [tSwitch isOn]; self.previewCell.preView.textStampHaveDate = _textStampHaveDate; [self.previewCell preViewReDisplay]; if (_textStampHaveDate || _textStampHaveTime) { self.navigationItem.rightBarButtonItem = self.rightItem; } else { self.navigationItem.rightBarButtonItem = [_textStampText isEqualToString:@""]? nil:self.rightItem; } } - (void)switchValueChanged_Time:(id)sender { UISwitch *tSwitch = (UISwitch *)sender; _textStampHaveTime = [tSwitch isOn]; self.previewCell.preView.textStampHaveTime = _textStampHaveTime; [self.previewCell preViewReDisplay]; if (_textStampHaveDate || _textStampHaveTime) { self.navigationItem.rightBarButtonItem = self.rightItem; } else { self.navigationItem.rightBarButtonItem = [_textStampText isEqualToString:@""]? nil:self.rightItem; } } - (void)textFieldWithText_Text:(id)sender { UITextField *textField = (UITextField *)sender; if (!_textStampText) { [_textStampText release]; _textStampText = nil; } _textStampText = [textField.text copy]; if (_textStampHaveDate || _textStampHaveTime) { self.navigationItem.rightBarButtonItem = self.rightItem; } else { self.navigationItem.rightBarButtonItem = [_textStampText isEqualToString:@""]? nil:self.rightItem; } //刷新preview NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0]; StampTextCell *tCell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath]; tCell.preView.textStampText = _textStampText; [tCell preViewReDisplay]; } - (void)buttonItemClicked_Color:(id)sender { UIButton *tBtn = (UIButton *)sender; _textStampStyle = tBtn.tag; NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0]; StampTextCell *tCell = (StampTextCell *)[_tableView cellForRowAtIndexPath:indexPath]; [tCell.preView setTextStampStyle:_textStampStyle]; [tCell preViewReDisplay]; } @end