您的位置:首页 > 产品设计 > UI/UE

iOS开发 -- UITextField

2015-08-14 14:40 543 查看
UITextField(输⼊入框):是控制⽂本输⼊入和显示的控件。在App中UITextField

出现频率也⽐较高。

UITextField和UILabel相⽐,UILabel主要⽤用于⽂字显⽰,不能编辑,UITextField允许⽤户编辑文字(输入)。

属性:

1:文本显示

text 要显⽰的⽂本内容 textField.text = @“lanoukeji”;

textColor ⽂本内容的颜⾊色 textField.textColor = [UIColor redColor]; textAlignment ⽂本的对⻬方式(⽔平方向) textField.textAlignment = NSTextAlignmentLeft; font ⽂文本字体 textField.font = [UIFont fontWithName:@“Helvetica- Bold” size:20];// 黑体加粗, 20 号字。

placeholder 占位字符串(没有任何输⼊时,给 出的提⽰字符串) textField.placeholder = @“ 请输⼊⽤户名 ”;

2:输入控制

enabled 是否允许输⼊入 textField.enabled =NO;// 不允许输⼊ , 不弹出建键盘 textField.enabled =YES;// 默认是 YES 。允许输⼊

clearsOnBeginEditing 是否开始输⼊的时候清空输入框内容 textField.clearsOnBeginEditing = YES;// 清空 textField.clearsOnBeginEditing = NO;// 不清空

secureTextEntry 是否⽂字以圆点格式显⽰textField.secureTextEntry = YES;// 密码模式 textField.secureTextEntry = NO;// 普通模式

keyboardType 弹出键盘的类型 ( 枚举值 ) textField.keyboardType = UIKeyboardTypeNumberPad; // 数字键盘

returnKeyType 键盘右下⾓ return 按钮类型(枚举 值) textField.returnKeyType = UIReturnKeyNext;

inputView ⾃定义输入视图(默认是键盘) textField.inputView = myInputView;

inputAccessoryView 输⼊视图上⽅方的辅助视图(默认 nil ) textField.inputAccessoryView = myAccessoryView;

3: 外观控制

borderStyle 边框样式(枚举值) textField.borderStyle = UITextBorderStyleRoundedRect;

clearButtonMode 清除按钮模式(枚举值) textField.clearButtonMode = UITextFieldViewModeAlways; // 总是显⽰清除按钮

leftView 输⼊框左视图 textField.leftView = leftView;

leftViewMode 左视图的显示模式 textField.leftViewMode = UITextFieldViewModeAlways; // 总是显示左视图

rightView 输⼊框右视图 textField.rightView = rightView;

rightViewMode 右视图的显⽰模式 textField.rightViewMode = UITextFieldViewModeAlways;

代码解释:

//1:创建对象
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 275, 30)];
//2: 配置属性
tf.backgroundColor = [UIColor whiteColor];
tf.background = [UIImage imageNamed:@"Snip20150814_2.png"];
//   tf.disabledBackground = [UIImage imageNamed:@"1.png"];
//(1)文字显示
//a.显示内容
tf.text =@"duang";
//b.设置文字颜色
tf.textColor = [UIColor blueColor];
//c.对齐方式
tf.textAlignment = NSTextAlignmentCenter;
//d.字体
tf.font = [UIFont systemFontOfSize:20.0f];
//e.水印  占位提示符
tf.placeholder = @"你是谁?? 逗比";


//(2)输入控制
//a. 设置是否可以输入  (可以控制输入框 输入的时机)
tf.enabled = YES;
//b.开始输入的时候自动清空输入框的内容
tf.clearsOnBeginEditing = YES;
//c.键盘风格
tf.keyboardType = UIKeyboardTypeNamePhonePad;
//d.设置return键的风格
tf.returnKeyType = UIReturnKeyJoin;
//e.输入内容变成点
//  tf.secureTextEntry = YES;

//f. 设置输入弹出的视图(默认 是键盘)
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 300)];
view.backgroundColor = [UIColor cyanColor];
// tf.inputView = view;
//G.设置辅助输入视图
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 375, 75)];
lable.text = @"��大五大三的 ��";
// tf.inputAccessoryView = lable;


//3: 设置输入框的样式
//a.边框风格
tf.borderStyle = UITextBorderStyleRoundedRect;
//b.输入框中有个叉号,用于一次性删除输入框中的内容
tf.clearButtonMode = UITextFieldViewModeAlways;
//c.左视图
UILabel *leftView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 28)];

leftView.backgroundColor = [UIColor colorWithRed:100/255.0 green:124/255.0 blue:107/255.0 alpha:0.8];
leftView.text = @"用户名";
tf.leftView = leftView;
// leftView.
//d 左视图 模式
tf.leftViewMode = UITextFieldViewModeAlways;

//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
tf.adjustsFontSizeToFitWidth =YES;
//设置自动缩小的字体的大小
tf.minimumFontSize = 12;
//输入的首字母是否大写
tf.autocorrectionType = UITextAutocapitalizationTypeWords;
//键盘的外观
tf.keyboardAppearance = UIKeyboardAppearanceAlert;

//声明text的代理是我,我会去实现把键盘往下收的方法 这个方法在UITextFieldDelegate里所以我们要采用UITextFieldDelegate这个协议
tf.delegate = self;

//3:添加父视图
[self.window addSubview:tf];
//4:配置所有权
[tf release];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: