在创建UITextField 代码的时候,有时我们需要自定义其风格,比如在文本框中加入图标,改变文本字段的行为,可以通过重写的方法来将其实现,这些方法都会返回一个CGRect结构,该结构制定了文本字段每个部件的边界范围,可以修改placeHolder颜色,字体等;
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[super viewDidLoad];
UIView *baseView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
[baseView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"second.jpg"]]];
[self.view addSubview:baseView];
self.field3 = [[UITextField alloc]initWithFrame:CGRectMake(80,280,220, 40)];
[self.field3 setBorderStyle:UITextBorderStyleRoundedRect];//外框类型
self.field3 .placeholder = @"请输入邮箱地址";//默认显示的字
self.field3 .secureTextEntry = NO ;//是否以密码形式显示
self.field3 .autocorrectionType = UITextAutocorrectionTypeNo;//设置是否启动自动提醒更正功能
self.field3 .autocapitalizationType = UITextBorderStyleNone;
self.field3 .returnKeyType = UIReturnKeyDone;//键盘返回类型
self.field3 .clearButtonMode = UITextFieldViewModeWhileEditing;//编辑时会出现个修改X,点击可以将输入的所有内容清除
self.field3 .delegate = self;
self.field3 .keyboardType = UIKeyboardTypeDefault;//键盘显示类型
// field.userInteractionEnabled = NO;
self.field3 .contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置居中输入
//field.scroll Enabled = YES;//是否可以拖动
self.field3 .autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
UIImageView *imgv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"email_img.png"]];
imgv.frame = CGRectMake(5, 5, 30, 30);
UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 1,39,38)];
[leftView addSubview:imgv];
self.field3.leftView = leftView;
self.field3.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:self.field3 ];
self.field1 = [[UITextField alloc]initWithFrame:CGRectMake(80,340,220, 40)];
[self.field1 setBorderStyle:UITextBorderStyleRoundedRect];//外框类型
self.field1 .placeholder = @"请输入登陆账号";//默认显示的字
self.field1 .secureTextEntry = NO;//是否以密码形式显示
self.field1 .autocorrectionType = UITextAutocorrectionTypeNo;//设置是否启动自动提醒更正功能
self.field1 .autocapitalizationType = UITextBorderStyleNone;
self.field1 .returnKeyType = UIReturnKeyDone;//键盘返回类型
self.field1 .clearButtonMode = UITextFieldViewModeWhileEditing;//编辑时会出现个修改X,点击可以将输入的所有内容清除
self.field1 .delegate = self;
self.field1 .keyboardType = UIKeyboardTypeNumberPad;//键盘显示类型
// field.userInteractionEnabled = NO;
self.field1 .contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置居中输入
//field.scroll Enabled = YES;//是否可以拖动
self.field1 .autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
UIImageView *imgv1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"user_img.png"]];
imgv1.frame = CGRectMake(5, 5, 30, 30);
UIView *leftView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 1,39,38)];
[leftView1 addSubview:imgv1];
self.field1.leftView = leftView1;
self.field1.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:self.field1];
self.field2 = [[UITextField alloc]initWithFrame:CGRectMake(80,400,220, 40)];
[self.field2 setBorderStyle:UITextBorderStyleRoundedRect];//外框类型
self.field2 .placeholder = @"请输入登陆密码";//默认显示的字
self.field2 .secureTextEntry = YES;//是否以密码形式显示
self.field2 .autocorrectionType = UITextAutocorrectionTypeNo;//设置是否启动自动提醒更正功能
self.field2 .autocapitalizationType = UITextBorderStyleNone;
self.field2 .returnKeyType = UIReturnKeyDone;//键盘返回类型
self.field2 .clearButtonMode = UITextFieldViewModeWhileEditing;//编辑时会出现个修改X,点击可以将输入的所有内容清除
self.field2 .delegate = self;
self.field2 .keyboardType = UIKeyboardTypeDefault;//键盘显示类型
// field.userInteractionEnabled = NO;
self.field2 .contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置居中输入
//field.scroll Enabled = YES;//是否可以拖动
self.field2 .autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
UIImageView *imgv3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pass_img.png"]];
imgv3.frame = CGRectMake(5, 5, 30, 30);
UIView *leftView3 = [[UIView alloc]initWithFrame:CGRectMake(0, 1,39,38)];
[leftView3 addSubview:imgv3];
self.field2.leftView = leftView3;
self.field2.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:self.field2];
UIButton *land = [UIButton buttonWithType:UIButtonTypeCustom];
[land setAlpha:1.0f];
[land setTitle:@"确认注册" forState:(UIControlStateNormal)];
[land setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
[land.layer setMasksToBounds:YES];
[land setFrame:CGRectMake(120,480,120,40)];
[land.layer setCornerRadius:10.0f];
[land.layer setBorderWidth:2];
//设置边界颜色
CGColorSpaceRef colorSpaceRef= CGColorSpaceCreateDeviceRGB();
CGColorRef color = CGColorCreate(colorSpaceRef,(CGFloat[]){0,0,1,1});
[land.layer setBorderColor:color];
[land addTarget:self action:@selector(land2:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:land];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)land2:(UIButton *) land
{
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
//当开始点击textField会调用的方法
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
//当textField编辑结束时调用的方法
}
//– clearButtonRectForBounds: //重写来重置clearButton位置,改变size可能导致button的图片失真
-(CGRect)clearButtonRectForBounds:(CGRect)bounds //控制清除按钮的位置
{
return CGRectMake(bounds.origin.x + bounds.size.width - 50, bounds.origin.y + bounds.size.height -20, 16, 16);
}
//– placeholderRectForBounds: //重写来重置占位符区域
-(CGRect)placeholderRectForBounds:(CGRect)bounds //控制placeHolder的位置,左右缩20
{
return CGRectInset(bounds, 20, 0);
//CGRect inset = CGRectMake(bounds.origin.x+250, bounds.origin.y, bounds.size.width -10, bounds.size.height); //等同于上面的代码方便理解bounds的定义;
//return inset;
}
//– textRectForBounds: 重写来重置文字区域
-(CGRect)textRectForBounds:(CGRect)bounds //控制显示文本的位置
{
return CGRectInset(bounds, 50, 0);
// CGRect inset = CGRectMake(bounds.origin.x+190, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
// return inset;
}
//– editingRectForBounds: //重写来重置编辑区域
-(CGRect)editingRectForBounds:(CGRect)bounds //控制编辑文本的位置
{
return CGRectInset( bounds, 10 , 0 );
// CGRect inset = CGRectMake(bounds.origin.x +10, bounds.origin.y, bounds.size.width -10, bounds.size.height);
// return inset;
}
- (CGRect)leftViewRectForBounds:(CGRect)bounds //控制左视图位置
{
// CGRect inset = CGRectMake(bounds.origin.x +10, bounds.origin.y, bounds.size.width-250, bounds.size.height);
// return inset;
return CGRectInset(bounds,50,0);
}
//– drawPlaceholderInRect: //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了
- (void)drawPlaceholderInRect:(CGRect)rect //控制placeHolder的颜色、字体
{
[[UIColor orangeColor] setFill];
// [[self.field.placeholder ] drawInRect:rectwithFont:[UIFontsystemFontOfSize:20]];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
//按下Done按钮的调用方法,我们让键盘消失
[textField resignFirstResponder];
return YES;
}
@end
[