码迷,mamicode.com
首页 > 移动开发 > 详细

UITextField限制输入的字符个数。比如输入手机号时,只能是11位

时间:2015-12-28 23:14:04      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:

- (void)setupTextField
{
    UITextField *tf = [[UITextField alloc] init];
    
    tf.keyboardType = UIKeyboardTypeNumberPad;
    
    tf.frame = CGRectMake(80, 80, 200, 40);
    
    tf.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:tf];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldEditChange:) name:@"UITextFieldTextDidChangeNotification" object:tf];
}

- (void)textfieldEditChange:(NSNotification *)noti
{
    UITextField *textField = noti.object;
    
    if (textField.text.length > 11)
    {
        textField.text = [textField.text substringToIndex:11];
    }
    
}

 

UITextField限制输入的字符个数。比如输入手机号时,只能是11位

标签:

原文地址:http://www.cnblogs.com/fs-ios/p/5084217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!