限制UITextField手机号只能输入11位

来源:互联网 发布:淘宝购物车东西没了 编辑:程序博客网 时间:2024/06/11 01:11

//输入手机号码

-(UITextField *)phoneNumTF{

    if (!_phoneNumTF) {

        _phoneNumTF = [[UITextFieldalloc] initWithFrame:CGRectMake(0,65, kSCreen_width, 60)];

        _phoneNumTF.placeholder =@"请输入账号手机号码";

        _phoneNumTF.keyboardType =UIKeyboardTypeNumberPad;

        _phoneNumTF.font = [UIFontsystemFontOfSize:18];

        _phoneNumTF.backgroundColor = [UIColorwhiteColor];

        _phoneNumTF.delegate =self;

        _phoneNumTF.tag =100;

        UIView *paddingView1 = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 10, 60)];

        _phoneNumTF.leftView = paddingView1;

        _phoneNumTF.leftViewMode =UITextFieldViewModeAlways;

        [_phoneNumTFaddTarget:selfaction:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];

    }

    return_phoneNumTF;

}


- (void)textFieldDidChange:(UITextField *)textField

{

    if (textField == self.phoneNumTF) {

        if (textField.text.length >11) {

            textField.text = [textField.textsubstringToIndex:11];

        }

    }

}


0 0
原创粉丝点击