nameField = [[UITextField alloc] init];
nameField.frame = CGRectMake(35, 130, 250, 35);
nameField.delegate = self;
//边界风格
nameField.borderStyle = UITextBorderStyleRoundedRect;
//文字在竖直方向的位置
nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//没有输入时显示的文字,输入之后小时,作提示用
nameField.placeholder = @"please input your email:";
//键盘返回键类型
nameField.returnKeyType = UIReturnKeyDone;
//当编辑的时候有一个x,提示可以一次删除UITextField中全部内容,需要代理配合实现
nameField.clearButtonMode = UITextFieldViewModeWhileEditing;
//键盘类型
nameField.keyboardType = UIKeyboardTypeEmailAddress;
//标记
nameField.tag = 1001;
//文本框中文本的颜色
nameField.textColor = [UIColor blueColor];
//设置文本框可以显示的最小字体,minimumFontSize,adjustsFontSizeToFitWidth必须配合使用才有效果
nameField.minimumFontSize = 8;
nameField.adjustsFontSizeToFitWidth = YES;
//输入文字的字体
nameField.font = [UIFont systemFontOfSize:20];
//只有至少在文本框输入一个字符后键盘的返回键才有效
nameField.enablesReturnKeyAutomatically = YES;
//密码风格
nameField.secureTextEntry = YES;
//设置大写
//None : 不设置大写
//Words : 每个单词首字母大写,这里的单词指的是以空格分开的字符串
//Sentances : 每个句子的第一个字母大写,这里的句子是以句号加空格分开的字符串
//All Characters : 所以字母大写
nameField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
[self.view addSubview:nameField];
常用代理方法:
1.开始编辑:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
2.点击return按钮所做的动作:
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
3.是否一次完全清除:
- (BOOL)textFieldShouldClear:(UITextField *)textField;
4.编辑完成:
- (void)textFieldDidEndEditing:(UITextField *)textField;
文本为空的时候,send(发送)按钮不可用:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSMutableString *newValue = [[textField.text mutableCopy] autorelease];
[newValue replaceCharactersInRange:range withString:string];//string是当前输入的字符,newValue是当前输入框中的字符
if ([newValue length]== 0)
{
sendBtn.enabled = NO;
}
else
{
sendBtn.enabled = YES;
}
return YES;
}