UITextField 文本输入框
几部曲
创建、设置背景、设置文本及文本样式、设置清除按钮和左右视图、关于编辑,设置键盘
一,创建方式
UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 200, 200)];
CGPoint tfCenter = tf.center;
tfCenter.x = self.view.center.x;
tf.center = tfCenter;
二、背景样式
1、边框
UITextBorderStyleNone, //无边框
UITextBorderStyleLine, //矩形边框
UITextBorderStyleBezel, //矩形边框,(底座),带阴影效果
UITextBorderStyleRoundedRect //圆角矩形
tf.borderStyle = UITextBorderStyleRoundedRect;
通过layer来设置边框
tf.layer.borderWidth = 1.0;
tf.layer.borderColor = [UIColor blackColor].CGColor;
tf.layer.cornerRadius = 20;
2、背景图片
//设置图片的三种方式。
//1。这种方式创建的UIImage对象,会在内存中保存一个缓存
// 后续在想要创建同一张图片,系统会从缓存中取
// tf.background = [UIImage imageNamed:@"back2.jpg"];
//UIImage 的初始化方法有三个
//2,3两种创建方式,它不保存缓存
//2
// NSString * string = [[NSBundle mainBundle] pathForResource:@"back2" ofType:@"jpg"];
// tf.background = [[UIImage alloc] initWithContentsOfFile:string];
//3
// NSString * string = [[NSBundle mainBundle] pathForResource:@"back2" ofType:@"jpg"];
// NSData * data = [[NSData alloc] initWithContentsOfFile:string];
//
// UIImage * image = [[UIImage alloc] initWithData:data];
// tf.background = image;
三、设置文本和文本样式
//给TextField设置文本
tf.text = @"我是UITextField";
//设置占位符
tf.placeholder = @"人称TFBoys";
//设置字体和颜色
tf.font = [UIFont systemFontOfSize:30];
// tf.textColor = [UIColor redColor];
//对齐方式
tf.textAlignment = NSTextAlignmentLeft;
//自适应字体大小
tf.adjustsFontSizeToFitWidth = YES;
//设置可变情况下,最小文字大小
tf.minimumFontSize = 5;
其他设置方式
// NSAttributedString * aString = [[NSAttributedString alloc] initWithString:@"我是UITextField" attributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];
//创建一个可变的AttributedString
// NSMutableAttributedString * aString = [[NSMutableAttributedString alloc] initWithString:@"我是UITextField"];
//
// //将aString指定范围的内容的样式进行定制
// [aString addAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(2, 4)];
//
// tf.attributedText = aString;
四、清楚按钮和左右视图
1、清除按钮
// UITextFieldViewModeNever,默认没有按钮
//当TextField处于编辑状态的时候
// UITextFieldViewModeWhileEditing,
//当TextField处于非编辑状态的时候
// UITextFieldViewModeUnlessEditing,
//任何时候
// UITextFieldViewModeAlways
tf.clearButtonMode = UITextFieldViewModeAlways;
2、左右视图
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
label.text = @"密码";
tf.leftView = label;
tf.leftViewMode = UITextFieldViewModeAlways;
//设置右视图,当右视图显示的时候,会盖住ClearButton
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
imageView.image = [UIImage imageNamed:@"back2.jpg"];
tf.rightView = imageView;
tf.rightViewMode = UITextFieldViewModeWhileEditing;
五、关于编辑
//用代码设置TextField为编辑状态
//让函数的被调用对象,成为第一响应者。意味着输入框进入编辑状态
[tf becomeFirstResponder];
//当进入编辑状态,清除之前的内容
// tf.clearsOnBeginEditing = YES;
//当进入编辑状态,首次键入内容,会清除之前的内容
tf.clearsOnInsertion = YES;
//让TextField进入非编辑状态
[tf resignFirstResponder];
六、设置键盘
1、设置键盘外观
// UIKeyboardAppearanceDefault,
// UIKeyboardAppearanceDark
// UIKeyboardAppearanceLight
//设置键盘外观
tf.keyboardAppearance = UIKeyboardAppearanceLight;
2、设置键盘类型
//默认键盘
// UIKeyboardTypeDefault,
//适合输入ASCII码
// UIKeyboardTypeASCIICapable,
//适合输入数字和标点
// UIKeyboardTypeNumbersAndPunctuation,
//适合输入网址
// UIKeyboardTypeURL,
//适合输入数字
// UIKeyboardTypeNumberPad,
//适合输入电话号码
// UIKeyboardTypePhonePad,
//适合输入文字和电话
// UIKeyboardTypeNamePhonePad,
//
// UIKeyboardTypeEmailAddress,
//适合输入十进制数
// UIKeyboardTypeDecimalPad
//适合Twitter键盘
// UIKeyboardTypeTwitter
//适合网页搜索的键盘
// UIKeyboardTypeWebSearch
tf.keyboardType = UIKeyboardTypeDefault;
3、键盘return
// UIReturnKeyDefault,
// UIReturnKeyGo,
// UIReturnKeyGoogle,
// UIReturnKeyJoin,
// UIReturnKeyNext,
// UIReturnKeyRoute,
// UIReturnKeySearch,
// UIReturnKeySend,
// UIReturnKeyYahoo,
// UIReturnKeyDone,
// UIReturnKeyEmergencyCall,
tf.returnKeyType = UIReturnKeyEmergencyCall;
4、自定义键盘
// inputView 用于自定义键盘
UIView * inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1000, 150)];
tf.inputView = inputView;
// inputAccessoryView 用于自定义二级键盘
UIView * inputeAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
inputeAccessoryView.backgroundColor = [UIColor lightGrayColor];
tf.inputAccessoryView = inputeAccessoryView;