/*

UITextField文本输入框

*/

UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 275, 50)];

//设置边框形式

/*

UITextBorderStyleRoundedRect 圆角形式

UITextBorderStyleLine 线条形式

UITextBorderStyleBezel 槽形式

*/

textField.borderStyle = UITextBorderStyleRoundedRect;

//通常用于寻找当前文本输入框中显示的文字

textField.text = @"";

//文本颜色

textField.textColor = [UIColor redColor];

//设置文本字体大小

textField.font = [UIFont systemFontOfSize:20];

//设置背景颜色

textField.backgroundColor = [UIColor lightGrayColor];

//当重复开始编辑时候 清除文字

textField.clearsOnBeginEditing = YES;

//文字提示

textField.placeholder = @"请输入您的大区名字";

//文字密文(暗文) 该属性通常用于设置密码输入框

textField.secureTextEntry = NO;

//文字输入时的对齐方式

textField.textAlignment = NSTextAlignmentCenter;

//文字输入的清除按钮

/*

UITextFieldViewModeWhileEditing 当输入时

UITextFieldViewModeAlways 总是

UITextFieldViewModeUnlessEditing 不在输入时候

*/

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

//键盘的类型

textField.keyboardType = UIKeyboardTypeDefault;

//retuan键类型 可自定义键盘

textField.returnKeyType = UIReturnKeyJoin;

//左视图

UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

label.text = @"账号";

label.textAlignment = NSTextAlignmentCenter;

textField.leftView = label;

textField.leftViewMode = UITextFieldViewModeWhileEditing;

//右视图

UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[button setTitle:@"确定" forState:UIControlStateNormal];

button.frame = CGRectMake(0, 0, 50, 50);

[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

textField.rightView = button;

textField.rightViewMode = UITextFieldViewModeAlways;

[self.window addSubview:textField];

//让键盘产生第一响应 键盘会自动弹起

[textField becomeFirstResponder];

//收起键盘

/*

1、点击键盘的return键

2、点击Button

3、点击空白处弹回键盘

*/

  

/*

手势

*/

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];

自定制方法/手势方法

- (void)tapClick{

UITextField * textField = (UITextField*)[self.window viewWithTag:100];

[textField resignFirstResponder];

}

- (void)buttonClick:(UIButton*)button{

//取消第一响应

UITextField * textFiled = (UITextField*)[self.window viewWithTag:100];

[textFiled resignFirstResponder];

}

所有代理方法作用

//当Return键被点击时调用 通常用于收回键盘

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

[textField resignFirstResponder];

return YES;//5.1前设置NO为点击无效

}

//文本输入框开始输入时调用

- (void)textFieldDidBeginEditing:(UITextField *)textField{

//将键盘弹出

NSLog(@"开始输入");

}

//文本输入框结束输入时调用

- (void)textFieldDidEndEditing:(UITextField *)textField{

//获取当前文本输入框中所输入的文字

NSLog(@"所输入的内容为:%@",textField.text);

//例:判断账号书写形式是否正确 如果不正确提示填写错误 重新输入

NSLog(@"结束输入");

}

//文本输入框内容发生变化即会调用的方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

/*

NSLog(@"内容:%@",textField.text);//获取的是上一次所输入内容

NSLog(@"Location:%lu Length:%lu",range.location,range.length);//范围为当前文字的位置,长度为零

NSLog(@"==%@==",string);//实时获取当前输入的字符

*/

//需求 实时获取当前文本框中的所有文字

NSString * resultStr = [textField.text stringByAppendingString:string];

NSLog(@"%@",resultStr);

//可在该方法中判断所输入文字是否正确

return YES;

}

//了解

//是否允许文本输入框可以输入

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

return YES;

}

//是否允许文本输入框结束

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

//在该方法中可以通过判断文本长度限制键盘是否可以收回

return NO;

}

//是否允许被清除

- (BOOL)textFieldShouldClear:(UITextField *)textField{

NSLog(@"文字被清除");

return YES;

}

ios文本框基本使用,以及所有代理方法的作用的更多相关文章

  1. ios UITextField文本框基本使用,以及所有代理方法的作用

    /* UITextField文本输入框 */ UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50 ...

  2. css去除ios文本框默认圆角

    css去除ios文本框默认圆角 input, textarea {-webkit-appearance: none;}

  3. (三)在js(jquery)中获得文本框焦点和失去焦点的方法

    在js(jquery)中获得文本框焦点和失去焦点的方法   文章介绍两个方法和种是利用javascript onFocus onBlur来判断焦点和失去焦点,加一种是利用jquery $(" ...

  4. AngularJS进阶(三)HTML:让表单、文本框只读,不可编辑的方法

    HTML:让表单.文本框只读,不可编辑的方法 有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name=" ...

  5. jQuery监控文本框事件并作相应处理的方法

    本文实例讲述了jQuery监控文本框事件并作相应处理的方法.分享给大家供大家参考.具体如下: //事情委托 $(document)  .on('input propertychange', '#que ...

  6. js中对arry数组的各种操作小结 瀑布流AJAX无刷新加载数据列表--当页面滚动到Id时再继续加载数据 web前端url传递值 js加密解密 HTML中让表单input等文本框为只读不可编辑的方法 js监听用户的键盘敲击事件,兼容各大主流浏览器 HTML特殊字符

    js中对arry数组的各种操作小结   最近工作比较轻松,于是就花时间从头到尾的对js进行了详细的学习和复习,在看书的过程中,发现自己平时在做项目的过程中有很多地方想得不过全面,写的不够合理,所以说啊 ...

  7. HTML:让表单、文本框只读,不可编辑的方法

    有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...

  8. HTML中让表单input等文本框为只读不可编辑的方法

    有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...

  9. javascript:让表单 文本框 只读,不可编辑的方法

    有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...

随机推荐

  1. 使用Java注解开发自动生成SQL

    使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...

  2. 微信小程序支付步骤

    http://blog.csdn.net/wangsf789/article/details/53419781 最近开发微信小程序进入到支付阶段,一直以来从事App开发,所以支付流程还是熟记于心的.但 ...

  3. 通过jvisualvm查看JVM的状态

    安装了JDK之后,可以通过jvisualvm 命令进行性能分析

  4. eclipse设置JSP的默认编码

    有时候我们新建一个JSP页面,但是编码却不是我们想要的,我们可在eclipse里面进行如下设置: 点击eclipse上面的window-->preferences 输入查找jsp-->点击 ...

  5. 获取星座的JS函数

    通过JS获取星座的函数 function getConstellation(m,d){ var s="魔羯水瓶双鱼牡羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯"; var arr=[2 ...

  6. 【LeetCode】459. Repeated Substring Pattern

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  7. HDU 1054 Strategic Game(无向二分图的最大匹配)

    ( ̄▽ ̄)" //凡无向图,求匹配时都要除以2 #include<iostream> #include<cstdio> #include<algorithm&g ...

  8. 图的存储结构:邻接矩阵(邻接表)&链式前向星

    [概念]疏松图&稠密图: 疏松图指,点连接的边不多的图,反之(点连接的边多)则为稠密图. Tips:邻接矩阵与邻接表相比,疏松图多用邻接表,稠密图多用邻接矩阵. 邻接矩阵: 开一个二维数组gr ...

  9. Memcached 集群的高可用(HA)架构

    Memcache自身并没有实现集群功能,如果想用Memcahce实现集群需要借助第三方软件或者自己设计编程实现,这里将采用memagent代理实现,memagent又名magent,大家注意下,不要将 ...

  10. [kuangbin带你飞]专题四 最短路练习 POJ 1797 Heavy Transportation

    求每条道路的最大承载量 和上一道题差不多 就是松弛的规则从最大值变成了最小值 /* *********************************************** Author :Su ...