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

UITextField

2015-07-21 14:12 399 查看
UITextField
一、UITextField:文本输入的控件
1、初始化一个UITextField
UITextField *pswTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];

1、设置边框的类型
pswTextField.borderStyle = UITextBorderStyleRoundedRect;

2、提示的文字,当编辑时消失
pswTextField.placeholder = @"请输入密码";

3、设置键盘输入样式
pswTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;

4、设置键盘显示样式
pswTextField.keyboardAppearance = UIKeyboardAppearanceAlert;

5、设置成不是明文显示
pswTextField.secureTextEntry = YES;

6、设置键盘的返回样式
pswTextField.returnKeyType = UIReturnKeyNext;

7、设置清除按钮什么时候出现
pswTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

8、把需要放到TextField左边或者右边的视图赋值给TextField左边或者右边视图
pswTextField.leftView = left;
注:还需要设置左边或右边视图的样式
pswTextField.leftViewMode = UITextFieldViewModeAlways;

9、设置TextField的背景图片
pswTextField.background = [UIImage imageNamed:@“text.png”];

10、设置TextField禁用时的背景
pswTextField.disabledBackground = [UIImage imageNamed:@"text1.jpg"];
注:enabled:设置是否禁用(NO:禁用 YES:不禁用),默认YES
pswTextField.enabled = YES;

11、添加到View
[self.view addSubview:pswTextField];

二、代理:让别人帮忙做某件事(自己在本类实现不了的功能,让其他类帮忙实现)
1、如果要使用代理,需要先添加代理的协议
@interface ViewController : UIViewController<UITextFieldDelegate>
2、在使用的地方挂上代理(如果代理方法不执行,先检查是否挂上代理)
pswTextField.delegate = self;

三、方法:
1、已经开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSLog(@"已经开始编辑!");
}

2、可以得到用户输入的单独字符
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(@"%@", string);
return YES;
}

3、点击Return键的时候调用(标志已经编辑结束)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// 回收键盘
[textField resignFirstResponder];
return YES;
}

4、清空输入框内容的时候调用
- (BOOL)textFieldShouldClear:(UITextField *)textField {
NSLog(@"数据被清空啦~赶紧重新输入吧
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: