*UITextField的使用,和UITextView的区别在于,前者限定只能显示一行文字,而后者能显示多行。
示例代码:
//创建一个文本框:限制显示一行文字--此处初始化(创建)一个文本框,定义它的位置和大小
UITextField * textField = [[UITextFieldalloc]initWithFrame:CGRectMake(0,227,370,40)];
//添加到主视图
[self.viewaddSubview:textField];
//设置边框样式--borderStyle属性,设置边框样式
textField.borderStyle= UITextBorderStyleLine;
//设置文本框代理--可以在延展中找到默认代理
textField.delegate= self;
//清空属性--输入框右边会出现x,点击会删除已输入文字
textField.clearButtonMode=UITextFieldViewModeWhileEditing ;
// textField.clearButtonMode= UITextFieldViewModeAlways ;
//设置文本框中默认显示文字
textField.placeholder=@"默认显示文字";
//默认文字颜色--KVC
[textFieldsetValue:[UIColorredColor] forKeyPath:@"_placeholderLabel.textColor"];
//默认文字大小--KVC
[textFieldsetValue:[UIFontboldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
//设置代理
textField.setDelegate = self;
UITextField的几种代理方法:UITextFieldDelegate :
使用这些代理方法前需要提前设置代理:
import “ViewController.h”
@interface ViewController ()
@property(strong,nonatomic)UILabel *label;
textField.setDelegate = self;
方法详解:
1. 设置输入框是否可被修改,return no将无法修改,不出现键盘。默认yes可以修改
- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField;
2.点击输入框时执行此方法
- (void)textFieldDidBeginEditing:(UITextField*)textField;
3.是否允许结束编辑,允许的话,将会失去first responder 第一响应者(比如键盘)
- (BOOL)textFieldShouldEndEditing:(UITextField*)textField;
4.结束编辑(失去第一响应者时执行)
- (void)textFieldDidEndEditing:(UITextField*)textField;
5.得到用户输入字符
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string;
6.当用户全部清空的时候的时候会调用
- (BOOL)textFieldShouldClear:(UITextField*)textField;
7.点击Return键(标志着编辑已经结束了),隐藏键盘
- (BOOL)textFieldShouldReturn:(UITextField*)textField;
-》还有很多细节的属性,如果有兴趣可以继续尝试,通过点方法即可获得到