iOS- UITextField限制输入长度

限制输入长度的问题,在这里完美的解决了!

//先创建一个textField 和 一个button。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#import "ViewController.h"
 
@interface  ViewController ()<UITextFieldDelegate> {
     
     UITextField *currentTextFeild;
     UIButton    *touchButton;
}
 
@end
 
@implementation  ViewController
 
- ( void )viewDidLoad {
     [ super  viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
     
     UITextField *textFields = [[UITextField alloc] initWithFrame:CGRectMake(15, 50,  self .view.bounds.size.width-15*2, 40)];
     textFields.backgroundColor = [UIColor brownColor];
     textFields.layer.cornerRadius = 5;
     textFields.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
     textFields.leftViewMode = UITextFieldViewModeAlways; //这两行是为了不让Text太贴textField的左边
     textFields.placeholder = @ "请输入手机号" ;
     textFields.delegate =  self ;
     [ self .view addSubview:textFields];
     currentTextFeild = textFields;
     
     UIButton *enableButton = [UIButton buttonWithType:UIButtonTypeCustom];
     enableButton.frame = CGRectMake(15, 100,  self .view.bounds.size.width-15*2, 40);
     enableButton.layer.cornerRadius = 5;
     enableButton.backgroundColor = [UIColor grayColor];
     [enableButton setTitle:@ "没内容不可点击"  forState:UIControlStateNormal];
     [enableButton setTitle:@ "可以按了"  forState:UIControlStateSelected];
     [enableButton setTitle:@ "按下去了"  forState:UIControlStateHighlighted];
     enableButton.enabled =  NO ;
     [enableButton addTarget: self  action: @selector (btnClick) forControlEvents:UIControlEventTouchUpInside];
     [ self .view addSubview:enableButton];
     touchButton = enableButton;
}
 
- ( void )btnClick {
     
     
}

//设置textField代理 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma mark -  UITextFieldDelegate
- ( BOOL )textFieldShouldBeginEditing:(UITextField *)textField {
     
     return  YES ;
}
 
- ( void )textFieldDidEndEditing:(UITextField *)textField {
     
     
}
 
- ( BOOL )textField:(UITextField *)textField shouldChangeCharactersInRange:( NSRange )range replacementString:( NSString  *)string {
     
     //用来判断是否可以继续输入, - range.length是为了判断是否可以删除
     NSInteger  currentLength = textField.text.length - range.length + string.length;
     if  (currentLength > 11) {
         
         return  NO ;
     }
     
     //判断按钮是否可以enable = YES
     if  (currentTextFeild.text && currentTextFeild.text.length > 0 && currentLength > 0) {
         
         touchButton.enabled =  YES ;
         touchButton.selected =  YES ;
     } else  {
         
         touchButton.enabled =  NO ;
         touchButton.selected =  NO ;
     }
     
     if  (currentLength <= 0) {
         
         touchButton.enabled =  NO ;
         touchButton.selected =  NO ;
     }
     
     return  YES ;
}
 
- ( BOOL )textFieldShouldClear:(UITextField *)textField {
     
     if  (currentTextFeild.tag == 11 || currentTextFeild.tag == 12) {
         //手机号
         touchButton.enabled =  NO ;
         touchButton.selected =  NO ;;
     }
     
     return  YES ;
}
 
- ( BOOL )textFieldShouldReturn:(UITextField *)textField {
     
     [textField resignFirstResponder];
     
     return  YES ;
}

 只要有基础的,差不多都能看明白。。。

转载于:https://www.cnblogs.com/jiafuyang/p/4829120.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值