悟空记账

JZNumberKeyBoard.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // JZNumberKeyBoard.m
  3. // JIZHANG
  4. //
  5. // Created by kuxuan on 2018/1/25.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "JZNumberKeyBoard.h"
  9. @interface JZNumberKeyBoard()
  10. @property (nonatomic,strong)UIButton *completeButton;
  11. @property (nonatomic,assign)BOOL complete; //是否允许8位
  12. @property (nonatomic,assign)BOOL isFirst; //是否是第一次输入
  13. @end
  14. @implementation JZNumberKeyBoard
  15. + (instancetype)sharedBoard{
  16. static JZNumberKeyBoard *keyBoard;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. keyBoard = [[self alloc]init];
  20. [keyBoard setupWindow];
  21. });
  22. return keyBoard;
  23. }
  24. - (void)setupWindow{
  25. self.complete = NO;
  26. self.isFirst = YES;
  27. self.editString = [NSMutableString stringWithString:@""];
  28. self.window = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 200, SCREEN_WIDTH, 200)];
  29. // self.window.windowLevel = UIWindowLevelNormal + 33;
  30. // self.window.rootViewController = [[UIViewController alloc]init];
  31. // [self.window makeKeyAndVisible];
  32. NSArray *array1 = @[@"1",@"2",@"3",@"+",@"4",@"5",@"6",@"-"];
  33. for (int i = 0; i < 8; i++) {
  34. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  35. button.frame = CGRectMake(SCREEN_WIDTH/4*(i%4), 50*(i/4), SCREEN_WIDTH/4, 50);
  36. button.backgroundColor = [UIColor JZColorWithHex:0xffffff];
  37. button.titleLabel.font = FONT_SYS(20);
  38. [button setTitle:array1[i] forState:UIControlStateNormal];
  39. [button setTitleColor:[UIColor JZColorWithHex:0x333333] forState:UIControlStateNormal];
  40. button.tag = 777 + i;
  41. [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  42. button.layer.borderColor = [UIColor JZColorWithHex:0xd8d8d8].CGColor;
  43. button.layer.borderWidth = 0.5;
  44. [self.window addSubview:button];
  45. }
  46. NSArray *array2 = @[@"7",@"8",@"9",@".",@"0",@"ic_delete"];
  47. for (int i = 0; i < 6; i++) {
  48. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  49. button.frame = CGRectMake(SCREEN_WIDTH/4*(i%3), 50*(i/3) + 100, SCREEN_WIDTH/4, 50);
  50. button.backgroundColor = [UIColor JZColorWithHex:0xffffff];
  51. button.titleLabel.font = FONT_SYS(20);
  52. button.tag = 785 + i;
  53. [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  54. if (i == 5) {
  55. [button setImage:[UIImage imageNamed:@"ic_delete"] forState:UIControlStateNormal];
  56. }else{
  57. [button setTitle:array2[i] forState:UIControlStateNormal];
  58. [button setTitleColor:[UIColor JZColorWithHex:0x333333] forState:UIControlStateNormal];
  59. }
  60. button.layer.borderColor = [UIColor JZColorWithHex:0xd8d8d8].CGColor;
  61. button.layer.borderWidth = 0.5;
  62. [self.window addSubview:button];
  63. }
  64. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  65. button.frame = CGRectMake(SCREEN_WIDTH*3/4, 100, SCREEN_WIDTH/4, 100);
  66. button.backgroundColor = [UIColor JZColorWithHex:0xffffff];
  67. button.titleLabel.font = FONT_SYS(18);
  68. button.tag = 791;
  69. [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  70. [button setTitle:@"完成" forState:UIControlStateNormal];
  71. button.backgroundColor = [UIColor baseColor];
  72. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  73. button.layer.borderColor = [UIColor JZColorWithHex:0xd8d8d8].CGColor;
  74. button.layer.borderWidth = 0.5;
  75. [self.window addSubview:button];
  76. }
  77. - (void)buttonAction:(UIButton *)btn{
  78. if (self.isFirst == YES) {
  79. self.isFirst = NO;
  80. if (![btn.currentTitle isEqualToString:@"+"]&&![btn.currentTitle isEqualToString:@"-"]) {
  81. self.editString = @"";
  82. }
  83. }
  84. if ([self.editString containsString:@"+"]||[self.editString containsString:@"-"]) {
  85. UIButton *btn = [self.window viewWithTag:791];
  86. [btn setTitle:@"=" forState:UIControlStateNormal];
  87. }
  88. if ([btn.currentTitle isEqualToString:@"="]) {
  89. self.complete = YES;
  90. }
  91. if (self.complete == NO) {
  92. if (self.editString.length >= 8) {
  93. if ([btn.currentTitle isEqualToString:@"+"]||[btn.currentTitle isEqualToString:@"-"]) {
  94. }else if ([self.editString containsString:@"+"]||[self.editString containsString:@"-"]) {
  95. NSArray *stringArray = [self.editString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"+-"]];
  96. for (int i = 1;i < stringArray.count; i ++) {
  97. if ([stringArray[i] length] >= 8) {
  98. return;
  99. }
  100. }
  101. }else{
  102. if (btn.currentTitle.length != 0) {
  103. return;
  104. }
  105. }
  106. }
  107. }
  108. switch (btn.tag - 777) {
  109. //1
  110. case 0:
  111. self.editString = [self.editString stringByAppendingString:@"1"];
  112. break;
  113. //2
  114. case 1:
  115. self.editString = [self.editString stringByAppendingString:@"2"];
  116. break;
  117. //3
  118. case 2:
  119. self.editString = [self.editString stringByAppendingString:@"3"];
  120. break;
  121. //+
  122. case 3:{
  123. if (self.editString.length) {
  124. NSString *last = [self.editString substringFromIndex:self.editString.length-1];
  125. if ([last isEqualToString:@"+"]||[last isEqualToString:@"-"]) {
  126. }else{
  127. self.editString = [self.editString stringByAppendingString:@"+"];
  128. }
  129. }else{
  130. self.editString = [self.editString stringByAppendingString:@"+"];
  131. }
  132. }
  133. break;
  134. //4
  135. case 4:
  136. self.editString = [self.editString stringByAppendingString:@"4"];
  137. break;
  138. //5
  139. case 5:
  140. self.editString = [self.editString stringByAppendingString:@"5"];
  141. break;
  142. //6
  143. case 6:
  144. self.editString = [self.editString stringByAppendingString:@"6"];
  145. break;
  146. //-
  147. case 7:
  148. {
  149. if (self.editString.length) {
  150. NSString *last = [self.editString substringFromIndex:self.editString.length-1];
  151. if ([last isEqualToString:@"+"]||[last isEqualToString:@"-"]) {
  152. }else{
  153. self.editString = [self.editString stringByAppendingString:@"-"];
  154. }
  155. }else{
  156. self.editString = [self.editString stringByAppendingString:@"-"];
  157. }
  158. }
  159. break;
  160. //7
  161. case 8:
  162. self.editString = [self.editString stringByAppendingString:@"7"];
  163. break;
  164. //8
  165. case 9:
  166. self.editString = [self.editString stringByAppendingString:@"8"];
  167. break;
  168. //9
  169. case 10:
  170. self.editString = [self.editString stringByAppendingString:@"9"];
  171. break;
  172. //.
  173. case 11:{
  174. NSString *last;
  175. if (self.editString.length >= 1) {
  176. last = [self.editString substringFromIndex:self.editString.length-1];
  177. }
  178. if (self.editString.length == 0) {
  179. self.editString = [self.editString stringByAppendingString:@"0."];
  180. }else if ([last isEqualToString:@"+"]||[last isEqualToString:@"-"]){
  181. self.editString = [self.editString stringByAppendingString:@"0."];
  182. }
  183. else{
  184. self.editString = [self.editString stringByAppendingString:@"."];
  185. }
  186. }
  187. break;
  188. //0
  189. case 12:
  190. self.editString = [self.editString stringByAppendingString:@"0"];
  191. break;
  192. //去除
  193. case 13:
  194. {
  195. if (self.editString.length != 0) {
  196. self.editString = [self.editString substringToIndex:self.editString.length - 1];
  197. }
  198. }
  199. break;
  200. //完成
  201. case 14:
  202. {
  203. [self setCompleteAction:btn];
  204. }
  205. break;
  206. default:
  207. break;
  208. }
  209. if (btn.tag - 777 != 14) {
  210. [_delegate numberKeyBoardDidEditing:self text:self.editString];
  211. }
  212. if ([self.editString containsString:@"+"]||[self.editString containsString:@"-"]) {
  213. if (self.editString.length == 1) {
  214. self.editString = @"";
  215. }
  216. }
  217. self.complete = NO;
  218. }
  219. - (void)setCompleteAction:(UIButton *)btn{
  220. // self.complete = YES;
  221. if ([btn.currentTitle isEqualToString:@"="]) {
  222. if (![self.editString containsString:@"+"]&&![self.editString containsString:@"-"]) {
  223. self.editString = @"";
  224. }
  225. NSArray *stringArray = [self.editString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"+-"]];
  226. float result = 0;
  227. for (int i = 0; i < stringArray.count; i ++) {
  228. NSString *str = stringArray[i];
  229. if (str.length == 0) {
  230. [MBProgressHUD showError:@"输入错误"];
  231. return;
  232. }else{
  233. if ([self.editString containsString:@"+"]) {
  234. result = result + str.floatValue;
  235. }else{
  236. if (i == 0) {
  237. result = result + str.floatValue;
  238. }else{
  239. result = result - str.floatValue;
  240. }
  241. }
  242. }
  243. }
  244. self.editString = [NSString stringWithFormat:@"%.2f",result];
  245. [_delegate numberKeyBoardDidEditing:self text:self.editString];
  246. [btn setTitle:@"完成" forState:UIControlStateNormal];
  247. self.complete = YES;
  248. }else{
  249. [_delegate numberKeyBoardDidEndEditing:self text:self.editString];
  250. }
  251. }
  252. - (void)clear{
  253. self.editString = @"";
  254. self.complete = NO;
  255. self.isFirst = YES;
  256. }
  257. @end