1.UITextField的初始化和设置
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
显示keyboard:
[textField becomeFirstResponder];
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[textField becomeFirstResponder];
return YES;
}
隐藏keyboard
[textField resignFirstResponder];
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
****************************************************************************************************************************************************************************
新增加内容:
textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
xib中
选中文本框后找到clear Button 后,选择 Appears while editing
****************************************************************************************************************************************************************************
1.初始化textfield并设置位置及大小
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
2.设置输入框内容的字体样式和大小,字体颜色,对齐方式
text.textColor = [UIColor redColor];
text.text = @"输入框默认文字";
text.textAlignment = UITextAlignmentLeft;
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//垂直居中
3.一次性关闭所有键盘
[ [ [ UIApplication sharedApplication ] keyWindow] endEdidting:YES ]; //关闭键盘
/*
//设置圆角边框方法二,该方法可以设置任意控件圆角边框,可以定义圆角的弧度、颜色、边框等
CALayer *layer=[txtf1 layer];
//是否设置边框以及是否可见
[layer setMasksToBounds:YES];
//设置边框圆角的弧度
[layer setCornerRadius:5.0];
//设置边框线的宽
[layer setBorderWidth:1];
//设置边框线的颜色
[layer setBorderColor:[[UIColor blackColor] CGColor]];
*/
3.设置边框样式,只有设置了才会显示边框样式
text.borderStyle = UITextBorderStyleRounded
typedef enum {
4.设置输入框的背景颜色,此时设置为白色
text.backgroundColor = [UIColor whiteColor];
5.设置背景图片
text.background = [UIImage imageNamed:@"dd.png"];
6.设置选中背景图片
text.disabledBackground = [UIImage imageNamed:@"cc.png"];
7.当输入框没有内容时,水印提示
8.输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
typedef enum {
} UITextFieldViewMode;
9.输入内容为密码样式
text.secureTextEntry = YES;
10.是否纠错
text.autocorrectionType = UITextAutocorrectionType
typedef enum {
} UITextAutocorrectionType
11.再次编辑时候清空
text.clearsOnBeginEditing = YES;
12.设置键盘的样式
typedef enum {
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeEmailAddre
UIKeyboardTypeDecimalPad
} UIKeyboardType;
13.首字母是否大写
typedef enum {
} UITextAutocapitalization
14.键盘外观
textView.keyboardAppearance=UIKeyboardAppearanceDefa
typedef enum {
UIKeyboardAppearanceDefa
UIKeyboardAppearanceAler
} UIReturnKeyType;
15.return键变成什么键
typedef enum {
} UIReturnKeyType;
16.设置代理,把textfield加到视图中
text.delegate = self;
[self.window addSubview:text];
17.最右侧加图片(左侧类似)
typedef enum {
} UITextFieldViewMode;
18.按return键键盘往下收
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
}
//补充点新内容 (从网上看到的)
//限制输入的最大字数
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string isEqualToString:@"\n"]){
return YES;
}
NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (self.searchTextField == textField)
{
if ([aString length] > 5) {
textField.text = [aString substringToIndex:5];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"超过最大字数不能输入了"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
return NO;
}
}
return YES;
}
一下补充内容来源于网上:
重写绘制行为
除了UITextField对象的风格选项,你还可以定制化UITextField对象,为他添加许多不同的重写方法,来改变文本字段的显示行为。这些方法都会返回一个CGRect结构,制定了文本字段每个部件的边界范围。以下方法都可以重写。
– textRectForBounds: //重写来重置文字区域
– drawTextInRect:
//改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
– placeholderRectForBounds: //重写来重置占位符区域
– drawPlaceholderInRect: