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 中最经常使用的组件之中的一个.关于它也有各种各样的需求,这些需求是它本身没有提供 ...
随机推荐
- 原创开源项目HierarchyViewer for iOS 2.1 Beta新功能介绍
回顾 HierarchyViewer for iOS是我们发布的一个开源项目,采用GPL v3.0协议. HierarchyViewer for iOS可以帮助iOS应用的开发和测试人员,在没有源代码 ...
- 【入门】匈牙利算法+HNOI2006 hero超级英雄
一.关于匈牙利算法 匈牙利算法是由匈牙利数学家Edmonds提出的,用增广路径求二分图最大匹配的算法. 听起来高端,其实说白了就是: 假设不存在单相思(单身狗偷偷抹眼泪),在一个同性恋不合法的国家里( ...
- android 修改framework下资源文件后如何编译
在framework/base/core/res/res 下添加资源文件后需要先编译资源 然后编译framework 才可正常引用 进入项目根目录 cd frameworks/base/core/re ...
- python打怪之路【第二篇】:ImportError: No module named setuptools
在python安装第三方模块时出现如下错误: python错误:ImportError: No module named setuptools这句错误提示的表面意思是:没有setuptools的模块, ...
- nodejs系列(二)REPL交互解释 事件循环
一.REPL交互解释 命令行中输入node启动REPL: > var x =2;undefined> do{x++;... console.log("x:="+x);. ...
- build.xml配置编译打包过程(转)
工程目录如下,使用eclipse中的ant对此工程进行编译打包: MonServer | --------src | |--------com | |--- ...
- es6箭头函数中this
普通函数: $scope.$on('$stateChangeSuccess',function(){this.list = this.getList();}); 箭头函数: $scope.$on('$ ...
- angular router-ui
将模块注入到控制器中的方法: 1.export module 2.在router中resolve解决: 2.1 resolve中直接return值 /*ngInject*/ worker : 'hi' ...
- 远程实时调试手机上的Web页面
1. 安装 需要Node.js平台, 先安装好后, 打开Node.js command prompt, 通过NPM来安装 weinre npm -g install weinre 2. 启动 ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries
题目链接 分析:01trie树,很容易就看出来了,也没什么好说的.WA了一发是因为没有看见如果数字位数大于01序列的时候01序列也要补全0.我没有晚上爬起来打,白天发现过的人极多. /******** ...