iOS--对字符串长度的判断

来源:互联网 发布:5g网络股票 编辑:程序博客网 时间:2024/06/06 20:15

//***textField代理方法***//

textFieldTel满足11位

textFieldTest满足仅4位

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    if (textField ==textFieldTel )

    {

        if(range.location==11)

        {

            return NO;

        }

        if (range.location==10||(range.location==11&&range.length==1))

        {

            if ( textFieldTest.text.length>=4)

            {

                btnTel.backgroundColor=publickOrange;

                btnTel.userInteractionEnabled=YES;

            }

            else

            {

               

                btnTel.backgroundColor=publickOrange;

                btnTel.userInteractionEnabled=YES;

            }

        }

        else

        {

            btnTel.backgroundColor=[UIColorlightGrayColor];

            btnTel.userInteractionEnabled=NO;

        }

        if (range.location==10&&range.length==1)

        {

            btnTel.backgroundColor=[UIColorlightGrayColor];

            btnTel.userInteractionEnabled=NO;

        }

    }

    if (textField == textFieldTest)

    {

        if (range.location >3||(range.length>4&&range.length==1))

        {

            return NO;

        }

        if (range.location>=3||(range.location>=3&&range.length==1))

        {

            if (textFieldTel.text.length==11)

            {

                btnTel.backgroundColor=publickOrange;

                btnTel.userInteractionEnabled=YES;

            }

            else

            {

                btnTel.backgroundColor=[UIColorlightGrayColor];

                btnTel.userInteractionEnabled=NO;

            }

        }

        else

        {

            btnTel.backgroundColor=[UIColorlightGrayColor];

            btnTel.userInteractionEnabled=NO;

        }

        if (range.location<=3&&range.length==1)

        {

            btnTel.backgroundColor=[UIColorlightGrayColor];

            btnTel.userInteractionEnabled=NO;

        }

    }

    return YES;

}

0 0
原创粉丝点击