HWTFCodeView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // CodeTextDemo
  3. //
  4. // Created by 小侯爷 on 2018/9/20.
  5. // Copyright © 2018年 小侯爷. All rights reserved.
  6. //
  7. #import "HWTFCodeView.h"
  8. @interface HWTFCodeView ()
  9. @property (nonatomic, assign) NSInteger itemCount;
  10. @property (nonatomic, assign) CGFloat itemMargin;
  11. @property (nonatomic, weak) UITextField *textField;
  12. @property (nonatomic, weak) UIControl *maskView;
  13. @property (nonatomic, strong) NSMutableArray<UILabel *> *labels;
  14. @property (nonatomic, strong) NSMutableArray<UIView *> *lines;
  15. @end
  16. @implementation HWTFCodeView
  17. #pragma mark - 初始化
  18. - (instancetype)initWithCount:(NSInteger)count margin:(CGFloat)margin
  19. {
  20. if (self = [super init]) {
  21. self.itemCount = count;
  22. self.itemMargin = margin;
  23. [self configTextField];
  24. }
  25. return self;
  26. }
  27. - (void)configTextField
  28. {
  29. self.backgroundColor = [UIColor clearColor];
  30. self.labels = @[].mutableCopy;
  31. self.lines = @[].mutableCopy;
  32. UITextField *textField = [[UITextField alloc] init];
  33. textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
  34. textField.keyboardType = UIKeyboardTypeNumberPad;
  35. [textField addTarget:self action:@selector(tfEditingChanged:) forControlEvents:(UIControlEventEditingChanged)];
  36. // 小技巧:这个属性为YES,可以强制使用系统的数字键盘,缺点是重新输入时,会清空之前的内容
  37. // clearsOnBeginEditing 属性并不适用于 secureTextEntry = YES 时
  38. // textField.secureTextEntry = YES;
  39. [self addSubview:textField];
  40. self.textField = textField;
  41. // 小技巧:通过textField上层覆盖一个maskView,可以去掉textField的长按事件
  42. UIButton *maskView = [UIButton new];
  43. maskView.backgroundColor = HEXCOLOR(0x181E1F);
  44. [maskView addTarget:self action:@selector(clickMaskView) forControlEvents:(UIControlEventTouchUpInside)];
  45. [self addSubview:maskView];
  46. self.maskView = maskView;
  47. for (NSInteger i = 0; i < self.itemCount; i++)
  48. {
  49. UILabel *label = [UILabel new];
  50. label.textAlignment = NSTextAlignmentCenter;
  51. label.textColor = [UIColor whiteColor];
  52. label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:41.5];
  53. [self addSubview:label];
  54. [self.labels addObject:label];
  55. }
  56. for (NSInteger i = 0; i < self.itemCount; i++)
  57. {
  58. UIView *line = [UIView new];
  59. line.backgroundColor = [UIColor lightGrayColor];
  60. [self addSubview:line];
  61. [self.lines addObject:line];
  62. }
  63. [self clickMaskView];
  64. }
  65. - (void)layoutSubviews
  66. {
  67. [super layoutSubviews];
  68. if (self.labels.count != self.itemCount) return;
  69. CGFloat temp = self.bounds.size.width - (self.itemMargin * (self.itemCount - 1));
  70. CGFloat w = temp / self.itemCount;
  71. CGFloat x = 0;
  72. for (NSInteger i = 0; i < self.labels.count; i++)
  73. {
  74. x = i * (w + self.itemMargin);
  75. UILabel *label = self.labels[i];
  76. label.frame = CGRectMake(x, 0, w, self.bounds.size.height);
  77. UIView *line = self.lines[i];
  78. line.frame = CGRectMake(x, self.bounds.size.height - 1, w, 1);
  79. }
  80. self.textField.frame = self.bounds;
  81. self.maskView.frame = self.bounds;
  82. }
  83. #pragma mark - 编辑改变
  84. - (void)tfEditingChanged:(UITextField *)textField
  85. {
  86. if (textField.text.length > self.itemCount) {
  87. textField.text = [textField.text substringWithRange:NSMakeRange(0, self.itemCount)];
  88. }
  89. for (int i = 0; i < self.itemCount; i++)
  90. {
  91. UILabel *label = [self.labels objectAtIndex:i];
  92. if (i < textField.text.length) {
  93. label.text = [textField.text substringWithRange:NSMakeRange(i, 1)];
  94. } else {
  95. label.text = nil;
  96. }
  97. }
  98. // 输入完毕后,自动隐藏键盘
  99. if (textField.text.length >= self.itemCount) {
  100. [textField resignFirstResponder];
  101. if ([self.delegate respondsToSelector:@selector(codeView:inputFnish:)]) {
  102. [self.delegate codeView:self inputFnish:textField.text];
  103. }
  104. }
  105. }
  106. - (void)clickMaskView
  107. {
  108. [self.textField becomeFirstResponder];
  109. }
  110. - (BOOL)endEditing:(BOOL)force
  111. {
  112. [self.textField endEditing:force];
  113. return [super endEditing:force];
  114. }
  115. - (NSString *)code
  116. {
  117. return self.textField.text;
  118. }
  119. @end