一、UITextField通知(UITextField文本发生变化会调用通知)
初始化一个(连线)UITextField控件
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
在viewDidLoad初始化通知
//注意name和object的设定值[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.pwdField];
实现通知方法
/** * 文本框的文字发生改变的时候调用 */ - (void)textChange { //在这里面当self.pwdFiled控件文字发生变化就会掉用这个通知方法。}
二、UITextField的代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing. - (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder - (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end - (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text - (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications) - (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when ‘return‘ key pressed. return NO to ignore.
三、UITextField的属性
//创建UITextField对象 UITextField * YM=[[UITextField alloc]init]; //设置文字[email protected]"欲眠"; //设置UITextField的文字颜色 YM.textColor=[UIColor redColor]; //设置UITextField的文本框背景颜色 YM.backgroundColor=[UIColor grayColor]; //设置UITextField的边框的风格 YM.borderStyle=UITextBorderStyleRoundedRect; //设置UITextField的代理 YM.delegate=self; //设置UITextField的文字对齐方式 YM.textAlignment=UITextAlignmentCenter;//居中对齐 YM.textAlignment=UITextAlignmentLeft;//左对齐 YM.textAlignment=UITextAlignmentRight;//右对齐 YM.textAlignment=UITextAlignmentFill;//填充对齐 //设置UITextField的文字大小和字体 YM.font=[UIFont fontWithName:@"Times New Roman" size:20]; //设置UITextField自适应文本框大小 YM.adjustsFontSizeToFitWidth=YES/NO;//自适应宽度 YM.adjustsFontSizeToFitHeight=YES/NO;//自适应高度 //设置UITextField是否拥有一键清除的功能 YM.clearsOnBeginEditing=YES/NO; //设置一键清除按钮是否出现 YM.clearButtonMode=UITextFieldViewModeNever; //设置UITextField的初始隐藏文字 [email protected]"输入密码"; //当UITextField的样式为UITextBorderStyleNone的时候,修改背景图片 YM.background=[UIImage imageNamed:@"xx.png"]; //设置UITextField的左边view YM.leftView=xxx; //设置UITextField的左边view出现模式 YM.leftViewMode=UITextFieldViewModeAlways; //设置UITextField的右边view YM.rightView=xxx; //设置UITextField的右边view出现模式 YM.rightViewMode=UITextFieldViewModeAlways; //设置UITextField的字的摆设方式 YM.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
时间: 2024-10-05 09:07:42