iOS UITextField输入框的简单demo

先看效果图

 二话不说直接说给你代码

@interface MineNewFamilyViewCell()<UITextFieldDelegate>


@property (nonatomic, strong) UITextField *textField;//输入框


[self.contentView addSubview:self.textField];//自己换添加的层

//我这里是mas的约束
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self); // 设置垂直Y居中
        make.left.equalTo(self.cellTitleLabel.mas_right).offset(16);
        make.right.equalTo(self.cellMapImageView.mas_left).offset(-16);
        make.height.mas_equalTo(20);
    }];


//关键的来了
 (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.borderStyle = UITextBorderStyleNone;
        _textField.returnKeyType = UIReturnKeyDone;//输入完成时的有下角按钮
        _textField.keyboardType = UIKeyboardTypeDefault;//键盘类型
        _textField.font = kPingfangRegularFont(14);
        _textField.textColor = UIColor.xl_titleColor2;
        _textField.backgroundColor = [UIColor clearColor];
        [_textField quickRoundedRectWith:0];
        
        UIView *leftview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 16, 50)];
        _textField.leftView = leftview;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        
        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 50)];
        UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        clearButton.frame = CGRectMake(0, 0, 40, 50);
        [clearButton setImage:XL_ImageNamed(@"input_text_delete") forState:UIControlStateNormal];
        [clearButton addTarget:self action:@selector(clearButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [rightView addSubview:clearButton];
        _textField.rightView = rightView;
        _textField.rightViewMode = UITextFieldViewModeWhileEditing;
        _textField.delegate = self; // 设置代理为自己
        
        [_textField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}

#pragma mark - Action
// 输入判断 
- (void)textFieldChange:(UITextField *)textField {
    
}
// 清空输入
- (void)clearButtonClicked{
    self.textField.text = @"";
}
#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];//收起键盘
    return YES;
}
//输入过程中监听
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSUInteger length = text.length;
    // 根据需要执行相应的操作,比如限制输入长度等
    if (length > 12) { // 假设MAX_LENGTH是你设定的最大长度
        [XLToast showWithTextOnWindow:XLString(@"限制12个中文字符")];
        return NO; // 阻止输入超过最大长度的字符
    }
    // 如果长度在允许范围内,正常处理输入
    return YES;
}
//输入完成后监听
- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSString *text = textField.text; // 获取文本字段的字符串
    NSUInteger length = text.length; // 获取字符串长度
    // 在这里可以根据字符串长度执行相应的操作
    NSLog(@"文本长度为: %lu", (unsigned long)length);
    if (length > 12) { // 假设MAX_LENGTH是你设定的最大长度
        [XLToast showWithTextOnWindow:XLString(@"限制12个中文字符")];
    }
}

 

posted on 2024-04-23 17:02  高彰  阅读(24)  评论(0编辑  收藏  举报

导航