根据用户是否输入和是否文本框内容为空来决定显示按钮颜色

简介: 根据用户是否输入和是否文本框内容为空来决定显示按钮颜色

根据用户是否输入和是否文本框内容为空来决定显示按钮颜色。

也可以实现按钮是否可以点击。

-(UIView *)inputTextFiled{
    if (!_inputTextFiled) {
        _inputTextFiled = [self createTextField];
        _inputTextFiled.keyboardType = UIKeyboardTypeDefault;
        _inputTextFiled.font = [UIFont systemFontOfSize:12];
        _inputTextFiled.backgroundColor = BGColorHex(F7F7F7);
        _inputTextFiled.frame = CGRectMake(10, 0, FULL_WIDTH-10*4 - 30 -60 -10, 30);
        NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:@"请文明发言以免禁言~" attributes:@{NSForegroundColorAttributeName:RGB(151, 151, 151)}];
        _inputTextFiled.attributedPlaceholder = placeholderString;
        _inputTextFiled.delegate = self;
        _inputTextFiled.userInteractionEnabled = YES;
        _inputTextFiled.clearButtonMode = UITextFieldViewModeAlways;
        //解决ios9及以下版本,点击键盘上方工具栏文字,按钮不变色问题
        @weakify(self);
        [[_inputTextFiled rac_signalForControlEvents:UIControlEventEditingChanged] subscribeNext:^(id x) {
            @strongify(self);
            if(isCommonUnitEmptyString(self.inputTextFiled.text))
            {
                self.sendButton.backgroundColor = RGB(196, 196, 196);
            }
            else
            {
                self.sendButton.backgroundColor = BGColorHex(F74490);
            }
        }];
    }
    return _inputTextFiled;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField == self.inputTextFiled)
    {
        if(isEmptyString(string))
        {
//            return YES;
            if(range.length < self.inputTextFiled.text.length)
            {
                return YES;
            }
            else
            {
                _sendButton.backgroundColor = RGB(196, 196, 196);
                return YES;
            }
        }
        else
        {
            if(messageMaxLength < self.inputTextFiled.text.length + string.length)
            {
                return NO;
            }
            else
            {
                _sendButton.backgroundColor = BGColorHex(F74490);
                return YES;
            }
        }
        
        return YES;
    }
    else
    {
        return YES;
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    //删除事件
    self.sendButton.backgroundColor = RGB(196, 196, 196);
    return YES;
}
目录
相关文章
|
10月前
textarea文本框默认显示文本鼠标点击时清空
textarea文本框默认显示文本鼠标点击时清空
|
3月前
|
前端开发 JavaScript
如何在文本域右下角设置字数限制提示
如何在文本域右下角设置字数限制提示
53 3
|
4月前
textarea文本框根据输入内容自动适应高度
textarea文本框根据输入内容自动适应高度
59 0
点击文字显示,点击文字隐藏(3)
点击文字显示,点击文字隐藏(2)
|
JavaScript
点击文字显示,点击文字隐藏(5)
点击文字显示,点击文字隐藏(5)
|
存储 前端开发 C++
2.8 输入控件(三)
2.8 输入控件(三)
2.8 输入控件(三)