UITextField及其方法

来源:互联网 发布:备案域名查询 编辑:程序博客网 时间:2024/06/05 11:34

// UITextField
// 可以输如文本的视图
// 调用协议方法需要先在类的声明 或实现后面签 协议, 并设置代理人

// 1.建立一个UITextField方式同UIView 1 ~ 4// 2.设置输入的字体的颜色textField.textColor = [UIColor blueColor];// 3.输入的文本的对其方式textField.textAlignment = NSTextAlignmentCenter;// 4.输入文本字体的大小textField.font = [UIFont systemFontOfSize:17];// 5.占位提示 输入内容后消失textField.placeholder = @"输入内容后消失";// 6.控制能否使用输入框 默认能(YES)textField.enabled = YES;// 7.密码效果(密码遮挡效果)textField.secureTextEntry = YES;// 8.键盘样式textField.keyboardType = UIKeyboardTypeURL;// 9.改变return按钮的样式textField.returnKeyType = UIReturnKeyNext;// 10.可以通过自定义视图,取代键盘textField.inputView = view;// 11.在屏幕下方放入视图, 并跟随键盘出现移动textField.inputAccessoryView = view;// 12.在输入框添加 清除按钮textField.clearButtonMode = UITextFieldViewModeAlways;// 13.给textField添加一个事件 (用textField去调用一个方法)[textField addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventEditingChanged];  <UIControlEventEditingChanged  当内容被修改时>// 14.设置输入框 边框颜色textField.layer.broderColor = [[UIColor redColor] CGColor];// 15.设置输入框 边框宽度textField.layer.borderWidth = 1.0f;// 实现点击return (触发条件)  回收键盘 (执行方法)- (BOOL)textFieldShouldReturn:(UITextField *)textField {    // 失去第一响应者 则回收键盘    [textField resignFirstResponder];    return YES;}// <在开始编辑室会触发这个方法> 实现textField位置过低时 屏幕上移方法- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {    // 如果textField的位置Y点(高低点)低于屏幕的一半    if (textField.frame.origin.y > HEIGTH / 2) {        // 计算当前textField和屏幕中心的距离        CGFloat height = textField.frame.origin.y - HEIGTH / 2;        // 改变整个视图的中心点位置 屏幕上移        self.view.center = CGPointMake(self.view.center.x, self.view.center.y - height);    }    return YES;}// <结束编辑的时候执行这个方法> 可以收回上移的屏幕- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{    if (textField.frame.origin.y > HEIGTH / 2) {        CGFloat heigth = textField.frame.origin.y - HEIGTH / 2;        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + heigth);        NSLog(@"视图回到原来位置");    }    return YES;}// 点击清除按钮会触发这个协议方法- (BOOL)textFieldShouldClear:(UITextField *)textField {    NSLog(@"内容清除");    return YES;}// 监听方法// 建立一个textField 绑定一个触发方法 即实现 监视输入的内容(包含累积的内容)[self.textField addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventEditingChanged];- (void)changeValue:(UITextField *)textField {    NSLog(@"%@", textField.text);}
0 0
原创粉丝点击