UITextField
//1.设置背景
//tf.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
//2.设置输入框的样式
/*
UITextBorderStyleNone, 默认样式,无样式
UITextBorderStyleLine, 矩形线条样式
UITextBorderStyleBezel, 刀锋线条样式
UITextBorderStyleRoundedRect 圆角样式
*/
tf.borderStyle = UITextBorderStyleRoundedRect;
//获取图片
UIImage *image = [UIImage imageNamed:@"btn_bg"];
//用区域的方式拉伸图片
image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height/2, image.size.width/2, image.size.height/2, image.size.width/2)];
//3.设置背景图片
tf.background = [UIImage imageNamed:@"btn_bg"];
//4.设置文字颜色
tf.textColor = [UIColor grayColor];
//5.设置文字的字体和大小
tf.font = [UIFont systemFontOfSize:20];
//6.设置对齐方式
//tf.textAlignment = NSTextAlignmentCenter;
//7.文字自适应输入框的宽度
tf.adjustsFontSizeToFitWidth = YES;
//8.设置最小文字自适应度
tf.minimumFontSize = 5;
//9.设置textField左边的图片,见图
UIImageView *leftImageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 25 )];
leftImageV.image = [UIImage imageNamed:@"username_icon"];
tf.leftView = leftImageV;
/*
UITextFieldViewModeNever, 不显示
UITextFieldViewModeWhileEditing, 当编辑状态时显示
UITextFieldViewModeUnlessEditing,不是编辑状态显示
UITextFieldViewModeAlways 始终显示
*/
tf.leftViewMode = UITextFieldViewModeAlways ;
//设置右边图片,道理同上面的设置左边图片
//tf.rightView
//tf.rightViewMode
//10.设置占位符,即一开始输入框里面的文字,当我们输入值的时候就没了
//tf.placeholder = @"请输入用户名";
//修改占位符的颜色和文字大小
tf.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"请输入用户名" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blueColor]}];
//11.显示右边的清空按钮,见图
/*
UITextFieldViewModeNever, 都不显示
UITextFieldViewModeWhileEditing, 当编辑时显示
UITextFieldViewModeUnlessEditing, 不编辑时显示
UITextFieldViewModeAlways 一直显示
*/
tf.clearButtonMode = UITextFieldViewModeWhileEditing
//12.安全输入,适用于密码输入
tf.secureTextEntry = NO;
//13.当输入状态时自动清空
tf.clearsOnBeginEditing = YES;
//14.不允许编辑,很少使用这个方法
//tf.enabled = NO;
//15.设置键盘样式
//tf.keyboardType = UIKeyboardTypeNumberPad;
//16.设置返回按钮的样式,有好多种样式,如图,这里就不一一演示了,大家可以自己实现看下
tf.returnKeyType = UIReturnKeyJoin;
//17.是否纠错,比如当你想要输入“Hello”时,打字打成“Hollo”,系统会自动帮你纠错
/*
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
*/
tf.autocorrectionType = UITextAutocorrectionTypeYes;
UITextFieldDelegate的协议方法
//开始编辑时调用
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
//已经开始编辑
-(void)textFieldDidBeginEditing:(UITextField *)textField
//清空内容时被调用
-(BOOL)textFieldShouldClear:(UITextField *)textField
//点击了return按钮被调用
-(BOOL)textFieldShouldReturn:(UITextField *)textField
//当输入文字时被调用
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString: (NSString *)string
//已经结束编辑
-(void)textFieldDidEndEditing:(UITextField *)textField