/*

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. setcookie 设置无效

    转载请署名 achieverain,谢谢 经常见人问PHP程序无法设置cookie.下面把我遇见过的情况都说一遍 1 PHP程序在执行setcookie之前有输出. 解决 :    把setcooki ...

  2. pro asp.net mvc5

    mvc 架构的每一个部分都是定义良好和自包含的,称为关注分离.域模型和控制器逻辑与UI是松耦合的.模型中操作数据的逻辑仅包含在模型中,显示数据的逻辑仅包含在视图中,而处理用户请求和用户输入的代码仅包含 ...

  3. 用sql实现汉字转拼音

    有时我们会需要将汉字转为拼音,例如需要将省市转为拼音后当做编码存储(尽管国家有统一的标识码,但有时候我们还是会用到),网络上也有工具提供汉字转拼音的功能,但各有优劣,一般转拼音后还会存在带声调的字母, ...

  4. java JDBC测试

    下载mysql-connector-java-5.1.31.jar添加到工程buildpath中 package com.jdbc.test; import java.sql.DriverManage ...

  5. Debian7安装php5.5/5.6

    ### 1 添加源 echo "deb http://packages.dotdeb.org wheezy-php56 all" >> /etc/apt/sources ...

  6. sql server 自定义split 标值函数

    自定义一个函数,分隔一个以分隔符的隔开字符串,例如把'1,3,5,7,9' 变成 数字1 3 5 7 9的结果集. 自定义标值函数: ),)) )) --实现split功能 的函数 as begin ...

  7. linux 命令展示该目录下的所有子目录及文件结构 tree

    1. apt-get install tree 2. tree -d -L 1 解释: tree :显示目录树: -d : 只显示目录: -L 1 : 选择显示的目录深度为1 , 只显示一层深度. 目 ...

  8. C# 常用接口学习 IComparable 和 IComparer

    C# 常用接口学习 IComparable 和 IComparer 作者:乌龙哈里 时间:2015-11-01 平台:Window7 64bit,Visual Studio Community 201 ...

  9. jquery里面的attr和css来设置轮播图竟然效果不一致

    /*封装$*/ // window.$=HTMLElement.prototype.$=function(selector){ // var elems=(this==window?document: ...

  10. 给hexo添加评论系统

    默认主题 landscape 文件目录,comments为新建的 _config.yml layout -- _partial -- article.ejs |- comments -- disqus ...