StampTextCell.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. //
  2. // StampTextCell.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 "StampTextCell.h"
  11. @implementation StampTextCell
  12. @synthesize delegate = _delegate;
  13. @synthesize cellType = _cellType;
  14. @synthesize preView = _preView;
  15. @synthesize stampTextField = _stampTextField;
  16. @synthesize haveTimeSwitch = _haveTimeSwitch;
  17. @synthesize haveDateSwitch = _haveDateSwitch;
  18. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  19. if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  20. }
  21. return self;
  22. }
  23. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  24. [super setSelected:selected animated:animated];
  25. // Configure the view for the selected state
  26. }
  27. - (void)dealloc {
  28. _delegate = nil;
  29. if (_stampTextField) {
  30. _stampTextField.delegate = nil;
  31. [_stampTextField release];
  32. }
  33. if (_preView) {
  34. [_preView removeFromSuperview];
  35. [_preView release];
  36. }
  37. [btnBlack release];
  38. [btnGreen release];
  39. [btnRed release];
  40. [btnBlue release];
  41. [super dealloc];
  42. }
  43. #pragma mark - Custom Cell
  44. - (void)setCellStyle:(CustomTextCellType)cellType label:(NSString *)label {
  45. if (self.stampTextField) {
  46. self.stampTextField.delegate = nil;
  47. }
  48. self.stampTextField = nil;
  49. if (_preView) {
  50. [_preView removeFromSuperview];
  51. [_preView release];
  52. _preView = nil;
  53. }
  54. _cellType = cellType;
  55. switch (cellType)
  56. {
  57. case CustomTextCellType_Text:
  58. {
  59. self.textLabel.text = [label isEqualToString:@""] ? @"StampText":label;
  60. self.selectionStyle = UITableViewCellSelectionStyleNone;
  61. CGRect tFieldRect = CGRectMake(0.0, 0.0f, 200.0f, 31.0f);
  62. UITextField *tTextField = [self textFieldItemCreate_Text:tFieldRect];
  63. tTextField.delegate = self;
  64. self.stampTextField = tTextField;
  65. self.accessoryView = tTextField;
  66. [tTextField release];
  67. break;
  68. }
  69. case CustomTextCellType_Color:
  70. {
  71. self.textLabel.text = label;
  72. self.selectionStyle = UITableViewCellSelectionStyleNone;
  73. CGFloat offset = 0;
  74. CGFloat tSpace = 58.0;
  75. btnBlack = [self buttonItemCreate:CGRectMake(offset, 0, 44, 44) colorType:TextStampColorType_Black];
  76. btnGreen = [self buttonItemCreate:CGRectMake(offset+=tSpace, 0, 44, 44) colorType:TextStampColorType_Green];
  77. btnRed = [self buttonItemCreate:CGRectMake(offset+=tSpace, 0, 44, 44) colorType:TextStampColorType_Red];
  78. btnBlue = [self buttonItemCreate:CGRectMake(offset+=tSpace, 0, 44, 44) colorType:TextStampColorType_Blue];
  79. offset = 0;
  80. leftGreen = [self buttonItemCreate:CGRectMake(offset+=tSpace, 44, 44, 44) colorType:TextStampColorType_GreenLeft];
  81. leftRed = [self buttonItemCreate:CGRectMake(offset+=tSpace, 44, 44, 44) colorType:TextStampColorType_RedLeft];
  82. leftBlue = [self buttonItemCreate:CGRectMake(offset+=tSpace, 44, 44, 44) colorType:TextStampColorType_BlueLeft];
  83. offset = 0;
  84. rightGreen = [self buttonItemCreate:CGRectMake(offset+=tSpace, 88, 44, 44) colorType:TextStampColorType_GreenRight];
  85. rightRed = [self buttonItemCreate:CGRectMake(offset+=tSpace, 88, 44, 44) colorType:TextStampColorType_RedRight];
  86. rightBlue = [self buttonItemCreate:CGRectMake(offset+=tSpace, 88, 44, 44) colorType:TextStampColorType_BlueRight];
  87. offset = offset + tSpace*2.0/3.0;
  88. UIView *tSelectView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, offset, 135)];
  89. [tSelectView addSubview:btnBlack];
  90. [tSelectView addSubview:btnGreen];
  91. [tSelectView addSubview:btnRed];
  92. [tSelectView addSubview:btnBlue];
  93. [tSelectView addSubview:leftGreen];
  94. [tSelectView addSubview:leftRed];
  95. [tSelectView addSubview:leftBlue];
  96. [tSelectView addSubview:rightGreen];
  97. [tSelectView addSubview:rightRed];
  98. [tSelectView addSubview:rightBlue];
  99. btnBlack.selected = YES;
  100. self.accessoryView = tSelectView;
  101. [tSelectView release];
  102. self.backgroundColor = [UIColor whiteColor];
  103. break;
  104. }
  105. case CustomTextCellType_Date:
  106. {
  107. self.textLabel.text = label;
  108. self.selectionStyle = UITableViewCellSelectionStyleNone;
  109. UISwitch *switchView = [self switchItemCreate];
  110. self.haveDateSwitch = switchView;
  111. [switchView release];
  112. self.backgroundColor = [UIColor whiteColor];
  113. break;
  114. }
  115. case CustomTextCellType_Time:
  116. {
  117. self.textLabel.text = label;
  118. self.selectionStyle = UITableViewCellSelectionStyleNone;
  119. UISwitch *switchView = [self switchItemCreate];
  120. self.haveTimeSwitch = switchView;
  121. [switchView release];
  122. self.backgroundColor = [UIColor whiteColor];
  123. break;
  124. }
  125. case CustomTextCellType_Preview:
  126. {
  127. self.textLabel.text = nil;
  128. self.haveTimeSwitch = nil;
  129. self.accessoryView = nil;
  130. self.selectionStyle = UITableViewCellSelectionStyleNone;
  131. //清除边框 背景
  132. self.backgroundColor = [UIColor clearColor];
  133. UIView *tView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
  134. self.backgroundView = tView;
  135. _preView = [[StampPreview alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
  136. _preView.textStampText = label;
  137. [self.contentView addSubview:_preView];
  138. break;
  139. }
  140. default:
  141. break;
  142. }
  143. }
  144. - (void)preViewReDisplay {
  145. [_preView setNeedsDisplay];
  146. }
  147. - (void)layoutSubviews {
  148. [super layoutSubviews];
  149. _preView.frame = CGRectMake(0, 0, self.frame.size.width, _preView.frame.size.height);
  150. }
  151. #pragma mark
  152. - (UITextField *)textFieldItemCreate_Text:(CGRect)rect {
  153. UITextField *tTextField = [[UITextField alloc] initWithFrame:rect];
  154. tTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  155. tTextField.returnKeyType = UIReturnKeyDone;
  156. tTextField.clearButtonMode = YES;
  157. [tTextField addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventEditingChanged];
  158. tTextField.placeholder = @"StampText";
  159. return tTextField;
  160. }
  161. - (UISwitch *)switchItemCreate {
  162. UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
  163. self.accessoryView = switchView;
  164. [switchView setOn:NO animated:NO];
  165. [switchView addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventValueChanged];
  166. return switchView;
  167. }
  168. - (UIButton *)buttonItemCreate:(CGRect)rect colorType:(TextStampColorType)type {
  169. UIImage *tImage = nil;
  170. switch (type) {
  171. case TextStampColorType_Black:
  172. tImage = [UIImage imageNamed:@"a_1.png"];
  173. break;
  174. case TextStampColorType_Green:
  175. tImage = [UIImage imageNamed:@"a_2.png"];
  176. break;
  177. case TextStampColorType_Red:
  178. tImage = [UIImage imageNamed:@"a_3.png"];
  179. break;
  180. case TextStampColorType_Blue:
  181. tImage = [UIImage imageNamed:@"a_4.png"];
  182. break;
  183. case TextStampColorType_GreenLeft:
  184. tImage = [UIImage imageNamed:@"a_2a.png"];
  185. break;
  186. case TextStampColorType_RedLeft:
  187. tImage = [UIImage imageNamed:@"a_3a.png"];
  188. break;
  189. case TextStampColorType_BlueLeft:
  190. tImage = [UIImage imageNamed:@"a_4a.png"];
  191. break;
  192. case TextStampColorType_GreenRight:
  193. tImage = [UIImage imageNamed:@"a_2b.png"];
  194. break;
  195. case TextStampColorType_RedRight:
  196. tImage = [UIImage imageNamed:@"a_3b.png"];
  197. break;
  198. case TextStampColorType_BlueRight:
  199. tImage = [UIImage imageNamed:@"a_4b.png"];
  200. break;
  201. default:
  202. break;
  203. }
  204. UIButton *tBtn = [[UIButton alloc] initWithFrame:rect];
  205. tBtn.tag = type;
  206. [tBtn setBackgroundColor:[UIColor clearColor]];
  207. [tBtn setBackgroundImage:[UIImage imageNamed:@"select_a.png"] forState:UIControlStateSelected];
  208. [tBtn setImage:tImage forState:UIControlStateNormal];
  209. [tBtn setImageEdgeInsets:UIEdgeInsetsMake(0.0, 4.0, 0, 4.0)];
  210. [tBtn addTarget:self action:@selector(buttonItemClicked:) forControlEvents:UIControlEventTouchUpInside];
  211. return tBtn;
  212. }
  213. #pragma mark UI Action
  214. - (void)doAction:(id)sender {
  215. switch (_cellType)
  216. {
  217. case CustomTextCellType_Text:
  218. if ([_delegate respondsToSelector:@selector(textFieldWithText_Text:)]) {
  219. [_delegate textFieldWithText_Text:sender];
  220. }
  221. break;
  222. case CustomTextCellType_Color:
  223. if ([_delegate respondsToSelector:@selector(buttonItemClicked_Color:)]) {
  224. [_delegate buttonItemClicked_Color:sender];
  225. }
  226. break;
  227. case CustomTextCellType_Date:
  228. if ([_delegate respondsToSelector:@selector(switchValueChanged_Date:)]) {
  229. [_delegate switchValueChanged_Date:sender];
  230. }
  231. break;
  232. case CustomTextCellType_Time:
  233. if ([_delegate respondsToSelector:@selector(switchValueChanged_Time:)]) {
  234. [_delegate switchValueChanged_Time:sender];
  235. }
  236. break;
  237. default:
  238. break;
  239. }
  240. }
  241. - (void)buttonItemClicked:(id)sender {
  242. btnBlack.selected = NO;
  243. btnGreen.selected = NO;
  244. btnRed.selected = NO;
  245. btnBlue.selected = NO;
  246. leftRed.selected = NO;
  247. leftBlue.selected = NO;
  248. leftGreen.selected = NO;
  249. rightRed.selected = NO;
  250. rightGreen.selected = NO;
  251. rightBlue.selected = NO;
  252. UIButton *tCurBtn = sender;
  253. tCurBtn.selected = YES;
  254. [self doAction:tCurBtn];
  255. }
  256. - (void)buttonItemClicked_GreenA:(id)sender {
  257. btnBlack.selected = NO;
  258. btnGreen.selected = YES;
  259. btnRed.selected = NO;
  260. btnBlue.selected = NO;
  261. [self doAction:btnGreen];
  262. }
  263. - (void)buttonItemClicked_RedA:(id)sender {
  264. btnBlack.selected = NO;
  265. btnGreen.selected = NO;
  266. btnRed.selected = YES;
  267. btnBlue.selected = NO;
  268. [self doAction:btnRed];
  269. }
  270. - (void)buttonItemClicked_BlueA:(id)sender {
  271. btnBlack.selected = NO;
  272. btnGreen.selected = NO;
  273. btnRed.selected = NO;
  274. btnBlue.selected = YES;
  275. [self doAction:btnBlue];
  276. }
  277. - (void)selectColor:(NSInteger)buttonColor {
  278. switch (buttonColor)
  279. {
  280. case TextStampColorType_GreenLeft:
  281. [self buttonItemClicked:leftGreen];
  282. break;
  283. case TextStampColorType_BlueLeft:
  284. [self buttonItemClicked:leftBlue];
  285. break;
  286. case TextStampColorType_RedLeft:
  287. [self buttonItemClicked:leftRed];
  288. break;
  289. case TextStampColorType_GreenRight:
  290. [self buttonItemClicked:rightGreen];
  291. break;
  292. case TextStampColorType_BlueRight:
  293. [self buttonItemClicked:rightBlue];
  294. break;
  295. case TextStampColorType_RedRight:
  296. [self buttonItemClicked:rightRed];
  297. break;
  298. case TextStampColorType_Green:
  299. [self buttonItemClicked:btnGreen];
  300. break;
  301. case TextStampColorType_Blue:
  302. [self buttonItemClicked:btnBlue];
  303. break;
  304. case TextStampColorType_Red:
  305. [self buttonItemClicked:btnRed];
  306. break;
  307. case TextStampColorType_Black:
  308. default:
  309. [self buttonItemClicked:btnBlack];
  310. break;
  311. }
  312. }
  313. #pragma mark - UITextFieldDelegate
  314. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  315. [textField resignFirstResponder];
  316. if ([_delegate respondsToSelector:@selector(textFieldWithText_Text:)]) {
  317. [_delegate textFieldWithText_Text:textField];
  318. }
  319. return YES;
  320. }
  321. @end