ios UITextField文本框基本使用,以及所有代理方法的作用
/*
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 UITextField文本框基本使用,以及所有代理方法的作用的更多相关文章
- ios文本框基本使用,以及所有代理方法的作用
/* UITextField文本输入框 */ UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50 ...
- (三)在js(jquery)中获得文本框焦点和失去焦点的方法
在js(jquery)中获得文本框焦点和失去焦点的方法 文章介绍两个方法和种是利用javascript onFocus onBlur来判断焦点和失去焦点,加一种是利用jquery $(" ...
- AngularJS进阶(三)HTML:让表单、文本框只读,不可编辑的方法
HTML:让表单.文本框只读,不可编辑的方法 有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name=" ...
- jQuery监控文本框事件并作相应处理的方法
本文实例讲述了jQuery监控文本框事件并作相应处理的方法.分享给大家供大家参考.具体如下: //事情委托 $(document) .on('input propertychange', '#que ...
- js中对arry数组的各种操作小结 瀑布流AJAX无刷新加载数据列表--当页面滚动到Id时再继续加载数据 web前端url传递值 js加密解密 HTML中让表单input等文本框为只读不可编辑的方法 js监听用户的键盘敲击事件,兼容各大主流浏览器 HTML特殊字符
js中对arry数组的各种操作小结 最近工作比较轻松,于是就花时间从头到尾的对js进行了详细的学习和复习,在看书的过程中,发现自己平时在做项目的过程中有很多地方想得不过全面,写的不够合理,所以说啊 ...
- IOS中文本框输入自动隐藏和自动显示
uilabe和UIText扩展方法 +(UILabel*)LabWithFrame:(CGRect)_rect text:(NSString*)aText textColor:(UIColor*)aC ...
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记35 UITextField文本框
本话来介绍UIKit框架中的组件UITextField. UItextField(文本框)和Label看起来看像,可是文本框是能够编辑的.在UI中使用文本框要注意.由于在模拟器上面输入文字是能够使用电 ...
- HTML:让表单、文本框只读,不可编辑的方法
有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...
- HTML中让表单input等文本框为只读不可编辑的方法
有时候,我们希望表单中的文本框是只读的,让用户不能修改其中的信息,如使<input type="text" name="input1" value=&qu ...
随机推荐
- 微软 Visual Studio 2017 中文正式版下载 – 免费社区版/专业版/企业版
作为“宇宙最强”的集成开发环境 IDE,微软的 Visual Studio 不仅破天荒发布了 macOS 版本,如今终于也推出了其 Windows 的最新版本—— VS 2017 正式版了.这对开发者 ...
- SpringBoot之自定义验证码
代码地址如下:http://www.demodashi.com/demo/14280.html 项目介绍 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控 ...
- PmExceptionMapper.xml 20160712
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- 关于android屏幕适配的问题(drawable-xxxxxxxx,dp,sp,px等等),偶尔看到了android源代码,关于dpi的区分的值
上一篇博客说了一下.9.png图片http://blog.csdn.net/qq_23195583/article/details/46737419 当然,点九的是指的能够进行拉伸的.那么假设图片不能 ...
- 【laravel5.4】发送alisms短信和163邮箱
public function test() { $res=ClientSource::all(); //dd($res); echo "<br>"; /* 发送短信[ ...
- windows安装tensorflow GPU
一.安装Anaconda Anaconda是Python发行包,包含了很多Python科学计算库.它是比直接安装Python更好的选择. 二.安装Tensorflow 如果安装了tensorflow, ...
- 2017年WorkApplication牛客网线上机试题
WorkApplication是一家日企,主要办公地在东京.新加坡.上海等地. 第一题:n的全排列中有多少个排列逆序数为k 输入两个数字n,k,两个数字的范围都是[1,1000]. 输出:n的全排列中 ...
- 【LeetCode】52. N-Queens II
N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...
- SDL 2.0 API by Category
Basics View information and functions related to... View the header Initialization and Shutdown SDL. ...
- Android清空Fragment回退栈
啊= =:国内的资料为什么都是抄来抄去的. 最后上了Stack Overflow才找到了正解. FragmentManager fragmentManager = getFragmentManager ...