134 lines
3.2 KiB
Objective-C
134 lines
3.2 KiB
Objective-C
//
|
|
// LoginVerifCodeView.m
|
|
// xplan-ios
|
|
//
|
|
// Created by 冯硕 on 2021/9/8.
|
|
//
|
|
|
|
#import "LoginVerifCodeView.h"
|
|
///第三方
|
|
#import <Masonry/Masonry.h>
|
|
#import <ReactiveObjC/ReactiveObjC.h>
|
|
///Tool
|
|
#import "ThemeColor.h"
|
|
|
|
@interface LoginVerifCodeView ()
|
|
///输入框的占位符
|
|
@property (nonatomic,strong) UITextField *textField;
|
|
///显示label的数组
|
|
@property (nonatomic,strong) NSMutableArray<UILabel *> *lableArray;
|
|
@end
|
|
|
|
|
|
@implementation LoginVerifCodeView
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
[self initSubViews];
|
|
[self initSubViewConstraints];
|
|
[self initEvents];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
#pragma mark - Private Method
|
|
- (void)initSubViews {
|
|
[self addSubview:self.textField];
|
|
}
|
|
|
|
- (void)initSubViewConstraints {
|
|
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.mas_equalTo(self);
|
|
}];
|
|
}
|
|
|
|
- (void)initEvents {
|
|
@weakify(self);
|
|
[[self.textField rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(__kindof UITextField *textField) {
|
|
@strongify(self);
|
|
[self updateLablesWithText:textField];
|
|
}];
|
|
}
|
|
|
|
- (void)updateLablesWithText:(UITextField *)textField {
|
|
if (textField.text.length > self.lableArray.count) {
|
|
textField.text = [textField.text substringToIndex:self.lableArray.count];
|
|
}
|
|
if (textField.text.length >= self.number) {
|
|
[textField resignFirstResponder];
|
|
if (_textFieldChangeBlock) {
|
|
_textFieldChangeBlock(textField.text);
|
|
}
|
|
}
|
|
|
|
for (UILabel *pwLab in self.lableArray) {
|
|
if (pwLab.tag < (100 + textField.text.length)) {
|
|
NSRange range = NSMakeRange(pwLab.tag-100, 1);
|
|
pwLab.text = [textField.text substringWithRange:range];
|
|
}else{
|
|
pwLab.text = @"";
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)configLabelArray {
|
|
CGFloat itemWidth = 45;
|
|
CGFloat itemHeight = 45;
|
|
CGFloat itemSpace = 11;
|
|
for (int i = 0; i < _number; i++) {
|
|
UILabel * label = [[UILabel alloc] init];
|
|
label.textColor = [ThemeColor mainTextColor];
|
|
label.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
|
|
label.textAlignment = NSTextAlignmentCenter;
|
|
label.backgroundColor = [ThemeColor appCellBackgroundColor];
|
|
label.layer.masksToBounds = YES;
|
|
label.layer.cornerRadius = 10;
|
|
label.tag = 100 + i;
|
|
[self addSubview:label];
|
|
[label mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.size.mas_equalTo(CGSizeMake(itemWidth, itemHeight));
|
|
make.centerY.mas_equalTo(self);
|
|
make.left.mas_equalTo(self).offset((itemWidth + itemSpace)* i);
|
|
}];
|
|
[self.lableArray addObject:label];
|
|
}
|
|
|
|
UILabel * label = [self.lableArray lastObject];
|
|
if (label) {
|
|
[self mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.right.mas_equalTo(label.mas_right);
|
|
make.height.mas_equalTo(itemHeight);
|
|
}];
|
|
}
|
|
}
|
|
|
|
#pragma mark - Getters And Setters
|
|
- (void)setNumber:(int)number {
|
|
_number = number;
|
|
if (_number > 0) {
|
|
[self configLabelArray];
|
|
}
|
|
}
|
|
|
|
- (UITextField *)textField {
|
|
if (!_textField) {
|
|
_textField = [[UITextField alloc] init];
|
|
_textField.tintColor = [UIColor clearColor];
|
|
_textField.textColor = [UIColor clearColor];
|
|
_textField.keyboardType = UIKeyboardTypeNumberPad;
|
|
[_textField becomeFirstResponder];
|
|
}
|
|
return _textField;
|
|
}
|
|
|
|
- (NSMutableArray<UILabel *> *)lableArray {
|
|
if (!_lableArray) {
|
|
_lableArray = [NSMutableArray array];
|
|
}
|
|
return _lableArray;
|
|
}
|
|
|
|
@end
|