UITextField是控制⽂字的输⼊和显⽰的控件。
所谓输⼊,在iOS⾥就是,点击输⼊框的时候,会弹出键盘,并能把键盘收回。
相⽐于UILabel,UITextField不仅能显⽰⽂字,更能输⼊⽂字。
创建UITextField遵循以下⼏个步骤:
1、alloc开辟空间,initWithFrame初始化与屏幕的⼤⼩。
2、设置UITextField的相关属性;
3、把UITextField添加到⽗视图,得以显⽰出来;
UITextField继承于UIControl,后者继承于UIview。所以,UITextField对象拥有
UIView和UIControl的所有⽅法和属性。
//
// ViewController.m
// UITextField01
//
// Created by cqy on 16/2/13.
// Copyright © 2016年 程清杨. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>{
UITextField *field;
UIView *view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
view = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 30, 60)];
view.backgroundColor = [UIColor yellowColor];
field = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 200, 50)];
field.delegate = self;
//属性
//borderStyle:输⼊框的边框样式,是⼀个枚举值。
field.borderStyle = UITextBorderStyleRoundedRect;
//text:属性text表⽰要显⽰的内容
//field.text = @"Hellow world !";//此时,在输⼊框中,有Hellow world !字样。这个text会获取输⼊框所有的⽂字。
//textColor:要显⽰的⽂本的颜⾊
field.textColor = [UIColor redColor];
//textAlignment:输⼊框⽂本的对齐⽅式(⽔平⽅向),默认左对齐
field.textAlignment = NSTextAlignmentLeft;
//fond:输⼊框中,字体的样式和⼤⼩
field.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
field.font = [UIFont systemFontOfSize:20];
//placholder:在输⼊框中没有⽂字时,placeholder提供了占位符,⽤以提⽰
field.placeholder = @"请输入!";
//enabled:是否允许输⼊框输⼊东西。返回值是BOOl类型。默认是YES,允许输⼊。
field.enabled = YES;
//clearsOnBeginEditing:是否开始输⼊的时候,清空输⼊框的⽂字内容。返回值是BOOL类型的。默认值是NO不清空。
field.clearsOnBeginEditing = YES;
//clearButtonMode:是否显示清空按钮
field.clearButtonMode = YES;
//secureTextEntry:是否输⼊的⽂字⽤圆点来代替显⽰(密码形式,默认NO)
field.secureTextEntry = YES;
//keyboardType:控制点击输⼊框时,弹出来的键盘是什么类型的键盘(枚举值)。
field.keyboardType = UIKeyboardTypeDefault;
//returnKeyType:弹出来的键盘最右下⾓的return键的类型。
field.returnKeyType = UIReturnKeySearch;
//inputView:定义输⼊视图(默认是键盘)。也就是说,在点击输⼊框的时候,⾃定义弹出来⼀个能输⼊的视图(键盘就是。
// field.inputView = view;
//inputAccessoryView:在弹出键盘的上⽅,⼀起弹出⼀个辅助视图,由我们⾃⼰定义辅助视图实现的功能。(默认是nil,也就是弹出来的视图上什么都没有)
//leftView:方框左视图leftView要与leftViewMode配套使⽤才能⽣效(对应rightView)。
field.leftViewMode = UITextFieldViewModeAlways;
field.leftView = view;
[self.view addSubview:field];
// Do any additional setup after loading the view, typically from a nib.
}
//点击return键触发事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[field resignFirstResponder];//解除第⼀响应者,把键盘回收。
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// ViewController.m
// UITextField01
//
// Created by cqy on 16/2/13.
// Copyright © 2016年 程清杨. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITextFieldDelegate>{
UITextField *field;
UIView *view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
view = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 30, 60)];
view.backgroundColor = [UIColor yellowColor];
field = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 200, 50)];
field.delegate = self;
//属性
//borderStyle:输⼊框的边框样式,是⼀个枚举值。
field.borderStyle = UITextBorderStyleRoundedRect;
//text:属性text表⽰要显⽰的内容
//field.text = @"Hellow world !";//此时,在输⼊框中,有Hellow world !字样。这个text会获取输⼊框所有的⽂字。
//textColor:要显⽰的⽂本的颜⾊
field.textColor = [UIColor redColor];
//textAlignment:输⼊框⽂本的对齐⽅式(⽔平⽅向),默认左对齐
field.textAlignment = NSTextAlignmentLeft;
//fond:输⼊框中,字体的样式和⼤⼩
field.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
field.font = [UIFont systemFontOfSize:20];
//placholder:在输⼊框中没有⽂字时,placeholder提供了占位符,⽤以提⽰
field.placeholder = @"请输入!";
//enabled:是否允许输⼊框输⼊东西。返回值是BOOl类型。默认是YES,允许输⼊。
field.enabled = YES;
//clearsOnBeginEditing:是否开始输⼊的时候,清空输⼊框的⽂字内容。返回值是BOOL类型的。默认值是NO不清空。
field.clearsOnBeginEditing = YES;
//clearButtonMode:是否显示清空按钮
field.clearButtonMode = YES;
//secureTextEntry:是否输⼊的⽂字⽤圆点来代替显⽰(密码形式,默认NO)
field.secureTextEntry = YES;
//keyboardType:控制点击输⼊框时,弹出来的键盘是什么类型的键盘(枚举值)。
field.keyboardType = UIKeyboardTypeDefault;
//returnKeyType:弹出来的键盘最右下⾓的return键的类型。
field.returnKeyType = UIReturnKeySearch;
//inputView:定义输⼊视图(默认是键盘)。也就是说,在点击输⼊框的时候,⾃定义弹出来⼀个能输⼊的视图(键盘就是。
// field.inputView = view;
//inputAccessoryView:在弹出键盘的上⽅,⼀起弹出⼀个辅助视图,由我们⾃⼰定义辅助视图实现的功能。(默认是nil,也就是弹出来的视图上什么都没有)
//leftView:方框左视图leftView要与leftViewMode配套使⽤才能⽣效(对应rightView)。
field.leftViewMode = UITextFieldViewModeAlways;
field.leftView = view;
[self.view addSubview:field];
// Do any additional setup after loading the view, typically from a nib.
}
//点击return键触发事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[field resignFirstResponder];//解除第⼀响应者,把键盘回收。
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end