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 ; } |
只要有基础的,差不多都能看明白。。。
iOS- UITextField限制输入长度的更多相关文章
- iOS 限制TextField输入长度(标准)
iOS 限制TextField输入长度(标准) 网上有很多限制textField输入长度方法,但是我觉得都不是很完美,准确来说可以说是不符合实际开发的要求,因此在这里整理一下textField限制输入 ...
- 更好地限制一个UITextField的输入长度
要限制一个UITextField的输入字数,首先想到的应该是通过 UITextFieldDelegate 的代理方法来限制: - (BOOL)textField:(UITextField *)text ...
- iOS UITextField限制输入数字
有时候项目中要求文本框中只能输入数字,如:价格.公里数.费用等等,一般的文本框不限制输入的格式,这时候只能强制限制输入框的输入格式了,代码如下: #import "ViewControlle ...
- UITextField限制输入长度
首先,汉字的输入时的联想词在输入到TextFiled时,并不会走 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersIn ...
- iOS UITextField限制输入字数
关于iOS的文本框有时需要限制字数,如手机号,在UITextField的代理单纯写一个判断,在字数超过限制时,这时再想删除就删除不掉,可以在代理这样写,就解决 - (BOOL)textField:(U ...
- UiTextField 限制输入长度
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...
- iOS 限制TextField输入长度
1 #import "MyInfoEditTableViewCell.h" @interface MyInfoEditTableViewCell()<UITextFieldD ...
- iOS 限制TextField输入长度(支持删除)
if (textField == _phoneTF) { //支持删除 && ) { return YES; } ) { _phoneTF.text = [textField.text ...
- 简单几步实现 IOS UITextField输入长度的控制
在ios开发过程中,我们有时候需要对UITextField的输入长度进行控制,比如输入手机号码最大长度为11位等,而ios自身又不像android那样可以设置输入框的输入长度,接下来通过简单几步实现这 ...
- 限制 UITextField 输入长度
限制 UITextField 输入长度 标签(空格分隔): UITextField UITextField 是 iOS 中最经常使用的组件之中的一个.关于它也有各种各样的需求,这些需求是它本身没有提供 ...
随机推荐
- Flowplayer-Setup
SOURCE URL: https://flowplayer.org/docs/setup.html 1. DOCTYPE At the top of your page declare the HT ...
- MyEclipse 10.7 添加JBOSS 6应用服务器
首先,确保你的JAVA和JBOSS的环境变量配置正确然后,在myeclipse中,window->preferences->myeclipse->servers->JBOSS- ...
- MSDN for VS2012 的安装
在VS2012中,由于MSDN默认不内置,VS2008 以上的就没有独立的 MSDN 了 ,而是被 Microsoft Help Viewer 取代了. 该组件包含在 VS2012 的 ISO 安装镜 ...
- 深入浅出设计模式——组合模式(Composite Pattern)
模式动机 对于树形结构,当容器对象(如文件夹)的某一个方法被调用时,将遍历整个树形结构,寻找也包含这个方法的成员对象(可以是容器对象,也可以是叶子对象,如子文件夹和文件)并调用执行.(递归调用)由于容 ...
- Java:描述反射机制的作用?举几个反射的应用?
比较全的解释了:JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方 ...
- 【转载】jQuery1.5之后的deferred对象详解
原文:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 原文作者 ...
- Android Hook Dexposed原理小析
dexposed是阿里巴巴在xposed框架上面开发的hotpatch一套框架 当然hotpatch的方式有很多,这里先介绍下dexposed原理 Demo中有个test函数, 在调用hook之前正常 ...
- 识别有效的IP地址和掩码并进行分类统
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; int c ...
- WebView注入Java对象注意事项
在android4.2以前,注入步骤如下: webview.getSetting().setJavaScriptEnable(true); class JsObject { public String ...
- java文件上传下载
文件上传首先要引入两个核心包 commons-fileupload-1.2.1.jar commons-io-1.4.jar 下面是对文件上传和下载的一些代码做的一个简单封装,可以方便以后直接使用[使 ...