Files
yinmeng-ios/xplan-ios/Main/Login/View/CustomView/LoginInputView.m
2023-01-17 15:41:59 +08:00

114 lines
2.9 KiB
Objective-C

//
// LoginInputView.m
// xplan-ios
//
// Created by 冯硕 on 2021/9/8.
//
#import "LoginInputView.h"
///第三方
#import <Masonry/Masonry.h>
///工具类
#import "ThemeColor.h"
@interface LoginInputView () <UITextFieldDelegate>
///容器
@property (nonatomic,strong) UIStackView *stackView;
///显示文本
@property (nonatomic,strong) UILabel *titleLabel;
///输入框
@property (nonatomic,strong) UITextField *textField;
@end
@implementation LoginInputView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.stackView];
[self.stackView addArrangedSubview:self.titleLabel];
[self.stackView addArrangedSubview:self.textField];
}
- (void)initSubViewConstraints {
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(50);
}];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self).offset(15);
make.right.top.bottom.mas_equalTo(self);
}];
}
#pragma mark - Getters And Setters
- (void)setPlaceHolder:(NSString *)placeHolder {
_placeHolder = placeHolder;
if (_placeHolder) {
self.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_placeHolder attributes:@{NSForegroundColorAttributeName: [ThemeColor secondTextColor], NSFontAttributeName:[UIFont systemFontOfSize:14]}];
}
}
- (void)setTitle:(NSString *)title {
_title = title;
if (_title) {
self.titleLabel.text = _title;
}
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisHorizontal;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentFill;
_stackView.spacing = 0;
}
return _stackView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.font = [UIFont systemFontOfSize:18];
_titleLabel.textColor = [ThemeColor mainTextColor];
_titleLabel.text = @"+86";
}
return _titleLabel;
}
- (UITextField *)textField {
if (!_textField) {
_textField = [[UITextField alloc] init];
_textField.textColor = [ThemeColor mainTextColor];
_textField.font = [UIFont systemFontOfSize:18.f];
_textField.borderStyle = UITextBorderStyleNone;
_textField.keyboardType = UIKeyboardTypeNumberPad;
_textField.tintColor = [ThemeColor appMainColor];
_textField.backgroundColor = [UIColor clearColor];
_textField.delegate = self;
}
return _textField;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return textField.text.length <= self.maxLength;
}
- (void)textFieldDidChangeSelection:(UITextField *)textField {
textField.text = [textField.text substringToIndex:MIN(textField.text.length, self.maxLength)];
}
@end