首页 > Computer > UITextField
2015
01-19

UITextField

//创建UITextField对象
UITextField * tf=[[UITextField alloc]init];

//设置UITextField的文字颜色
tf.textColor=[UIColor redColor];

//设置UITextField的文本框背景颜色
tf.backgroundColor=[UIColor grayColor];

//设置UITextField的边框的风格
tf.borderStyle=UITextBorderStyleRoundedRect;

//设置键盘类型为默认的
tf.keyboardType=UIKeyboardTypeDefault;
typedef enum {
UIKeyboardTypeDefault, 默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点
UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;

//首字母是否大写
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef enum {
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
} UITextAutocapitalizationType;

//返回键的类型
tf.returnKeyType=UIReturnKeyDefault;
typedef enum {
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;

//设置UITextField的代理
tf.delegate=self;

//设置UITextField的文字对齐方式
tf.textAlignment=UITextAlignmentCenter;//居中对齐
tf.textAlignment=UITextAlignmentLeft;//左对齐
tf.textAlignment=UITextAlignmentRight;//右对齐
tf.textAlignment=UITextAlignmentFill;//填充对齐

//设置UITextField的文字大小和字体
tf.font=[UIFont fontWithName:@”Times New Roman” size:20];

//设置UITextField自适应文本框大小
tf.adjustsFontSizeToFitWidth=YES/NO;//自适应宽度
tf.adjustsFontSizeToFitHeight=YES/NO;//自适应高度

//设置UITextField是否拥有一键清除的功能
tf.clearsOnBeginEditing=YES/NO;

//设置一键清除按钮是否出现
tf.clearButtonMode=UITextFieldViewModeNever;

//设置UITextField的初始隐藏文字
tf.placeholder=@”输入密码”;

//设置成密码格式 tf.secureTextEntry=YES;

//当UITextField的样式为UITextBorderStyleNone的时候,修改背景图片
tf.background=[UIImage imageNamed:@”xx.png”];

//设置UITextField的左边view
tf.leftView=xxx;

//设置UITextField的左边view出现模式
tf.leftViewMode=UITextFieldViewModeAlways;

//设置UITextField的右边view
tf.rightView=xxx;

//设置UITextField的右边view出现模式
tf.rightViewMode=UITextFieldViewModeAlways;

//设置UITextField的字的摆设方式
tf.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
聚焦方法 [tf becomeFirstResponder];

// 可以通过委托方法关闭键盘
-(BOOL)textFieldShouldReturn:(UITextField*)textField {
[tf resignFirstResponder]; return YES;
}

// 触摸背景来关闭虚拟键盘
// 给背景view添加UIControl触发点击事件
UIControl *_back = [[UIControlalloc] initWithFrame:self.view.frame];
_back.backgroundColor = [UIColorgrayColor];
self.view = _back;
[_back release];
[(UIControl *)self.viewaddTarget:selfaction:@selector(backgroundTap) forControlEvents:UIControlEventTouchDown];
//textfield
textField1 = [[UITextFieldalloc] initWithFrame:CGRectMake(20, 300, 200, 30)];
textField1.delegate = self;
[self.viewaddSubview:textField1];
textField2 = [[UITextFieldalloc] initWithFrame:CGRectMake(20, 30, 200, 30)];
textField2.delegate = self;
[self.viewaddSubview:textField2];

// 点击虚拟键盘上按键
UIToolbar *bar=[[UIToolbaralloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
UIBarButtonItem *btn=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCanceltarget:selfaction:@selector(resign)];
UIBarButtonItem *space=[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:selfaction:nil];//用于把btn挤到右侧
NSArray *btarr=[NSArrayarrayWithObjects:btn,space,nil];
bar.items=btarr;
//bar.tintColor=[UIColor blackColor];//给toolbar染色作用同下
bar.barStyle=UIBarStyleBlackTranslucent;//bar的类型设置半透明
self.textField1.inputAccessoryView=bar;
self.textField2.inputAccessoryView=bar;

// toolbar方法
-(void)resign{
[self.textField1resignFirstResponder];
[self.textField2resignFirstResponder];
}

// 点击关闭键盘
-(IBAction)backgroundTap {
//动画是执行推上textfield后还原
NSTimeInterval animationDuration = 0.30f;
[UIViewbeginAnimations:@”ResizeForKeyboard”context:nil];
[UIViewsetAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height); //还原view
self.view.frame = rect;
[UIViewcommitAnimations];

[textField1resignFirstResponder];
[textField2resignFirstResponder];
}

// 点击return关闭键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
//动画是执行推上textfield后还原
NSTimeInterval animationDuration = 0.30f;
[UIViewbeginAnimations:@”ResizeForKeyboard”context:nil];
[UIViewsetAnimationDuration:animationDuration];
CGRect rect = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, self.view.frame.size.height); //还原上一步view上提效果
self.view.frame = rect;
[UIViewcommitAnimations];
[textField resignFirstResponder];
return YES;
}

// 解决虚拟键盘挡住UITextField的方法,将整个self.view上推键盘顶部与textfield底部大小的距离即offset
– (void)textFieldDidBeginEditing:(UITextField *)textField {
CGRect frame = textField.frame;
int offset = frame.origin.y+60- (self.view.frame.size.height – 216.0);//求出键盘顶部与textfield底部大小的距离
NSTimeInterval animationDuration = 0.30f;
[UIViewbeginAnimations:@”ResizeForKeyBoard”context:nil];
[UIViewsetAnimationDuration:animationDuration];
float width = self.view.frame.size.width;
float height = self.view.frame.size.height;
if(offset > 0) {
CGRect rect = CGRectMake(0.0f, -offset,width,height); //上推键盘操作,view大小始终没变
self.view.frame = rect;
}
[UIViewcommitAnimations];
}

最后编辑:
作者:wy182000
这个作者貌似有点懒,什么都没有留下。