// // CodeTextDemo // // Created by 小侯爷 on 2018/9/20. // Copyright © 2018年 小侯爷. All rights reserved. // #import "HWTFCodeBView.h" @interface HWTFCodeBView () @property (nonatomic, assign) NSInteger itemCount; @property (nonatomic, assign) CGFloat itemMargin; @property (nonatomic, weak) UITextField *textField; @property (nonatomic, weak) UIControl *displayMaskView; @property (nonatomic, strong) NSMutableArray *labels; @end @implementation HWTFCodeBView #pragma mark - 初始化 - (instancetype)initWithCount:(NSInteger)count margin:(CGFloat)margin { if (self = [super init]) { self.itemCount = count; self.itemMargin = margin; [self configTextField]; } return self; } - (void)setOriginCode:(NSString *)originCode { _originCode = originCode; if (originCode.length > 0) { self.textField.text = originCode; for (NSInteger i = 0; i < self.labels.count; i++) { UILabel *label = self.labels[i]; if (originCode.length >= self.labels.count) { char a = [originCode characterAtIndex:i]; label.text = [NSString stringWithFormat:@"%c", a]; } } } } - (void)configTextField { self.backgroundColor = [UIColor whiteColor]; self.labels = @[].mutableCopy; UITextField *textField = [[UITextField alloc] init]; textField.autocapitalizationType = UITextAutocapitalizationTypeNone; textField.keyboardType = UIKeyboardTypeNumberPad; [textField addTarget:self action:@selector(tfEditingChanged:) forControlEvents:(UIControlEventEditingChanged)]; [self addSubview:textField]; self.textField = textField; UIButton *displayMaskView = [UIButton new]; displayMaskView.backgroundColor = [UIColor clearColor]; [displayMaskView addTarget:self action:@selector(clickMaskView) forControlEvents:(UIControlEventTouchUpInside)]; [self addSubview:displayMaskView]; self.displayMaskView = displayMaskView; for (NSInteger i = 0; i < self.itemCount; i++) { UILabel *label = [UILabel new]; label.textAlignment = NSTextAlignmentCenter; label.textColor = HEXCOLOR(0x333333); label.font = [UIFont systemFontOfSize:24 weight:UIFontWeightHeavy]; // label.layer.borderColor = [COLOR_999999 CGColor]; // label.layer.borderWidth = 1; label.backgroundColor = HEXCOLOR(0xF7F7F7); label.layer.cornerRadius = 8; label.layer.masksToBounds = YES; [self addSubview:label]; [self.labels addObject:label]; } } - (void)layoutSubviews { [super layoutSubviews]; if (self.labels.count != self.itemCount) return; CGFloat temp = self.bounds.size.width - (self.itemMargin * (self.itemCount - 1)); CGFloat w = temp / self.itemCount; CGFloat x = 0; for (NSInteger i = 0; i < self.labels.count; i++) { x = i * (w + self.itemMargin); UILabel *label = self.labels[i]; label.frame = CGRectMake(x, 0, w, self.bounds.size.height); } self.textField.frame = self.bounds; self.displayMaskView.frame = self.bounds; } #pragma mark - 编辑改变 - (void)tfEditingChanged:(UITextField *)textField { if (textField.text.length > self.itemCount) { textField.text = [textField.text substringWithRange:NSMakeRange(0, self.itemCount)]; } for (int i = 0; i < self.itemCount; i++) { UILabel *label = [self.labels objectAtIndex:i]; if (i < textField.text.length) { label.text = [textField.text substringWithRange:NSMakeRange(i, 1)]; } else { label.text = nil; } } // 输入完毕后,自动隐藏键盘 if (textField.text.length >= self.itemCount) { [textField resignFirstResponder]; } } - (void)clickMaskView { [self.textField becomeFirstResponder]; } - (BOOL)endEditing:(BOOL)force { [self.textField endEditing:force]; return [super endEditing:force]; } - (NSString *)code { return self.textField.text; } @end