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 中最经常使用的组件之中的一个.关于它也有各种各样的需求,这些需求是它本身没有提供 ...
随机推荐
- Failed to create the part's controls [eclipse]
查看源码 出现 Failed to create the part's controls 解决方法: eclipse.ini 中添加: -startup plugins/org.eclipse.eq ...
- C++string中有关大小和容量的函数浅析
1.length()与size() length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符 ...
- matlab求解二元一次方程组的解得表达式
- Java开发中经典的小实例-(能被3整除,并且十个数换一行)
import java.util.Scanner;public class Test15 { public static void main(String[] args) { // ...
- Ubuntu anzhuang
zhongwenshurubuzhidaozenmeqiehuan anhzuang Flash apt-get install flashplugin-installer
- hdu4734 F(x)
链接 这题当时在网络赛时很费劲的推出来的,以递推的形式写出来的,一些边界点特别不好控制,靠看数据改出来的.现在改出dfs形式,也是很简单的. 因为f(x)的数不会很大,直接保留前面枚举的数得出的结果即 ...
- JavaScript学习笔记及知识点整理_3
1.js的事件冒泡及阻止方法:事件冒泡的概念:在一个对象上触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事 ...
- 理解RxJava线程模型
RxJava作为目前一款超火的框架,它便捷的线程切换一直被人们津津乐道,本文从源码的角度,来对RxJava的线程模型做一次深入理解.(注:本文的多处代码都并非原本的RxJava的源码,而是用来说明逻辑 ...
- AxureRP8实战手册(基础1-10)
基础操作篇 本篇包含56种常见的基础操作,初学者应在掌握本篇内容后再进行实战案例篇的学习,以免产生学习障碍.同时,建议具备一定基础的读者学习本篇中相对生疏的内容,并加以掌握. 第1章 使用元件 本文目 ...
- break和continue的区别
break是结束整个循环体,continue是结束单次循环