Files
peko-ios/YuMi/Modules/YMLogin/View/CustomView/LoginInputItemView.m
2025-04-25 15:54:30 +08:00

387 lines
14 KiB
Objective-C

//
// LoginInputItemView.m
// YuMi
//
// Created by P on 2025/3/14.
//
#import "LoginInputItemView.h"
static NSString *const kAreaCodePrefix = @"+";
@interface LoginInputItemView() <UITextFieldDelegate>
@property(nonatomic, copy) NSString *placeholderText;
@property(nonatomic, assign) LoginInputType inputType;
@property(nonatomic, strong) UITextField *inputTextField;
@property(nonatomic, strong) UIButton *rightActionButton;
@property(nonatomic, strong) UIButton *areaCodeButton;
@property(nonatomic, strong) UILabel *areaLabel;
@property(nonatomic, strong) UIButton *verificationCodeButton;
@property(nonatomic, assign) NSInteger verificationCountDown;
@property(nonatomic, strong) NSTimer *verificationTimer;
@end
@implementation LoginInputItemView
- (void)deallo {
_verificationTimer = nil;
}
- (instancetype)initWithType:(LoginInputType)type {
if (self = [super init]) {
self.backgroundColor = [UIColor whiteColor];
[self setCornerRadius:23];
[self addSubview:self.inputTextField];
self.inputType = type;
}
return self;
}
- (void)setInputType:(LoginInputType)inputType {
_inputType = inputType;
switch (inputType) {
case LoginInputType_id: {
[self updatePlaceholder:YMLocalizedString(@"20.20.51_text_3")];
self.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
}
break;
case LoginInputType_email: {
[self updatePlaceholder:YMLocalizedString(@"20.20.51_text_4")];
self.inputTextField.keyboardType = UIKeyboardTypeEmailAddress;
}
break;
case LoginInputType_phoneNum: {
[self updatePlaceholder:YMLocalizedString(@"XPMineResetLoginPwdViewController1")];
self.inputTextField.keyboardType = UIKeyboardTypePhonePad;
}
break;
case LoginInputType_password: {
[self updatePlaceholder:YMLocalizedString(@"XPLoginPwdViewController3")];
self.inputTextField.keyboardType = UIKeyboardTypeDefault;
self.inputTextField.secureTextEntry = YES;
}
break;
case LoginInputType_createPassword:
[self updatePlaceholder:YMLocalizedString(@"20.20.51_text_11")];
self.inputTextField.keyboardType = UIKeyboardTypeDefault;
self.inputTextField.secureTextEntry = YES;
break;
case LoginInputType_verificationCode: {
[self updatePlaceholder:YMLocalizedString(@"20.20.51_text_7")];
self.inputTextField.keyboardType = UIKeyboardTypeDefault;
}
break;
case LoginInputType_confirmPassword: {
[self updatePlaceholder:YMLocalizedString(@"20.20.51_text_11")];
self.inputTextField.keyboardType = UIKeyboardTypeDefault;
}
break;
default:
break;
}
[self updateInputLayout];
}
- (void)updatePlaceholder:(NSString *)placeholder {
self.inputTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{
NSFontAttributeName: kFontRegular(14),
NSForegroundColorAttributeName: UIColorFromRGB(0xafb1b3)
}];
}
- (void)updateInputLayout {
switch (self.inputType) {
case LoginInputType_id:
case LoginInputType_email:
case LoginInputType_createPassword:
case LoginInputType_confirmPassword:
{
[self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(12, 20, 12, 20));
}];
}
break;
case LoginInputType_password: {
[self insertRightActionButton];
[self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self).offset(20);
make.bottom.top.mas_equalTo(self).inset(12);
make.trailing
.mas_equalTo(self.rightActionButton.mas_leading)
.offset(-20);
}];
}
break;
case LoginInputType_phoneNum: {
[self insertAreaCodeButton];
[self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.areaCodeButton.mas_trailing).offset(8);
make.bottom.top.mas_equalTo(self).inset(12);
make.trailing.mas_equalTo(self).offset(-12);
}];
}
break;
case LoginInputType_verificationCode: {
[self insertVerificationCodeButton];
[self.inputTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self).offset(20);
make.bottom.top.mas_equalTo(self).inset(12);
make.trailing
.mas_equalTo(self.verificationCodeButton.mas_leading)
.offset(-20);
}];
}
break;
default:
break;
}
}
- (void)insertAreaCodeButton {
[self addSubview:self.areaCodeButton];
[self.areaCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self);
make.leading.mas_equalTo(13);
make.height.mas_equalTo(20);
make.width.mas_equalTo(60);
}];
UIImageView *image = [[UIImageView alloc] initWithImage:kImage(@"login_page_area_arrow")];
image.contentMode = UIViewContentModeScaleAspectFit;
[self.areaCodeButton addSubview:image];
[self.areaCodeButton addSubview:self.areaLabel];
[self.areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.areaCodeButton);
make.leading.mas_equalTo(self.areaCodeButton);
make.height.mas_equalTo(20);
}];
[image mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.areaCodeButton);
make.leading.mas_equalTo(self.areaLabel.mas_trailing).offset(4);
make.size.mas_equalTo(CGSizeMake(10, 5));
}];
}
- (void)insertVerificationCodeButton {
[self addSubview:self.verificationCodeButton];
[self.verificationCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self);
make.trailing.mas_equalTo(-23);
make.height.mas_equalTo(20);
if (isMSRTL()) {
make.width.mas_equalTo(80);
} else {
make.width.mas_equalTo(60);
}
}];
}
- (void)insertRightActionButton {
[self addSubview:self.rightActionButton];
[self.rightActionButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self);
make.trailing.mas_equalTo(-20);
make.height.mas_equalTo(24);
make.width.mas_equalTo(24);
}];
}
- (NSString *)inputContent {
return self.inputTextField.text;
}
- (void)updateAreaCode:(NSString *)code {
self.areaLabel.text = [NSString stringWithFormat:@"%@%@", kAreaCodePrefix, code];
// if (_areaCodeButton) {
// [self.areaCodeButton setTitle:[NSString stringWithFormat:@"%@%@", kAreaCodePrefix, code]
// forState:UIControlStateNormal];
// }
}
- (NSString *)loadAreaCode {
NSString *code = [self.areaLabel text];
if ([code hasPrefix:kAreaCodePrefix]) {
[code stringByReplacingOccurrencesOfString:kAreaCodePrefix
withString:@""];
}
return code;
}
- (void)startVerificationCountDown {
self.verificationCountDown = 60;
@kWeakify(self);
self.verificationTimer = [NSTimer scheduledTimerWithTimeInterval:1
repeats:YES
block:^(NSTimer * _Nonnull timer) {
@kStrongify(self);
[self updateVerificationCountDown];
}];
}
- (void)endVerificationCountDown {
if (_verificationTimer) {
[self.verificationTimer invalidate];
}
self.verificationCodeButton.enabled = YES;
}
- (void)updateBGColor:(UIColor *)color {
self.backgroundColor = color;
}
- (void)displayKeyboard {
[self.inputTextField becomeFirstResponder];
}
#pragma mark -
- (void)handleVerificationCountDown {
if (_handleItemAction) {
self.handleItemAction(self.inputType);
}
}
- (void)updateVerificationCountDown {
[self.verificationCodeButton setTitle:[NSString stringWithFormat:YMLocalizedString(@"20.20.51_text_9"), @(self.verificationCountDown)]
forState:UIControlStateDisabled];
self.verificationCodeButton.enabled = NO;
self.verificationCountDown -= 1;
if (self.verificationCountDown < 0) {
[self endVerificationCountDown];
}
}
#pragma mark -
- (void)didTapAreaButton {
if (_handleItemAction) {
self.handleItemAction(self.inputType);
}
}
- (void)didTapVerificationButton {
if (_handleItemAction) {
self.handleItemAction(self.inputType);
}
}
- (void)didTapRightActionButton {
self.rightActionButton.selected = !self.rightActionButton.isSelected;
self.inputTextField.secureTextEntry = !self.rightActionButton.selected;
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidChange:(UITextField *)textField {
// NSLog(@"%@", textField.text);
switch (self.inputType) {
case LoginInputType_id:
case LoginInputType_email:
case LoginInputType_phoneNum:
if (self.handleFirstInputContentUpdate) {
self.handleFirstInputContentUpdate(textField.text);
}
break;
case LoginInputType_password:
case LoginInputType_verificationCode:
if (self.handleSecondInputContentUpdate) {
self.handleSecondInputContentUpdate(textField.text);
}
break;
case LoginInputType_confirmPassword:
if (self.handleThirdInputContentUpdate) {
self.handleThirdInputContentUpdate(textField.text);
}
break;
default:
break;
}
}
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// NSString *updatedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
// switch (self.inputType) {
// case LoginInputType_id:
// case LoginInputType_email:
// case LoginInputType_phoneNum:
// if (self.handleFirstInputContentUpdate) {
// self.handleFirstInputContentUpdate(updatedText);
// }
// break;
// case LoginInputType_password:
// case LoginInputType_verificationCode:
// if (self.handleSecondInputContentUpdate) {
// self.handleSecondInputContentUpdate(updatedText);
// }
// break;
// case LoginInputType_confirmPassword:
// if (self.handleThirdInputContentUpdate) {
// self.handleThirdInputContentUpdate(updatedText);
// }
// break;
// default:
// break;
// }
// });
// return YES;
//}
#pragma mark -
- (UITextField *)inputTextField {
if (!_inputTextField) {
_inputTextField = [[UITextField alloc] init];
_inputTextField.font = kFontMedium(14);
_inputTextField.textColor = UIColorFromRGB(0x313131);
// _inputTextField.delegate = self;
[_inputTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
return _inputTextField;
}
- (UILabel *)areaLabel {
if (!_areaLabel) {
_areaLabel = [UILabel labelInitWithText:[NSString getCountryCode] font:kFontMedium(14) textColor:UIColorFromRGB(0x313131)];
}
return _areaLabel;
}
- (UIButton *)areaCodeButton {
if (!_areaCodeButton) {
_areaCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_areaCodeButton addTarget:self
action:@selector(didTapAreaButton)
forControlEvents:UIControlEventTouchUpInside];
}
return _areaCodeButton;
}
- (UIButton *)verificationCodeButton {
if (!_verificationCodeButton) {
_verificationCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_verificationCodeButton.titleLabel setFont:kFontMedium(14)];
[_verificationCodeButton setTitle:YMLocalizedString(@"20.20.51_text_8") forState:UIControlStateNormal];
[_verificationCodeButton setTitleColor:UIColorFromRGB(0xff8c03) forState:UIControlStateNormal];
[_verificationCodeButton setTitleColor:UIColorFromRGB(0xafb1b3) forState:UIControlStateDisabled];
[_verificationCodeButton addTarget:self
action:@selector(didTapVerificationButton)
forControlEvents:UIControlEventTouchUpInside];
}
return _verificationCodeButton;
}
- (UIButton *)rightActionButton {
if (!_rightActionButton) {
_rightActionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_rightActionButton setBackgroundImage:kImage(@"login_page_kaigan") forState:UIControlStateNormal];
[_rightActionButton setBackgroundImage:kImage(@"login_page_eye_close") forState:UIControlStateSelected];
[_rightActionButton addTarget:self
action:@selector(didTapRightActionButton)
forControlEvents:UIControlEventTouchUpInside];
}
return _rightActionButton;
}
@end