UITextField的一些属性

来源:互联网 发布:java io深入 编辑:程序博客网 时间:2024/05/19 16:22

1.基本概念

 //7.设置水平方向文本对齐方式:默认左对齐

    textField0.textAlignment = NSTextAlignmentCenter;

    //8.设置竖直方向文本对齐方式:默认是center

    [textField0 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];

  //11.设置自动首字母大写类型

    [textField0 setAutocapitalizationType:UITextAutocapitalizationTypeWords];

    //12.设置自动纠错类型:默认是开启自动纠错

    [textField0 setAutocorrectionType:UITextAutocorrectionTypeNo];

    //13.设置键盘风格:数字、网址、电话、Email...

    [textField0 setKeyboardType:UIKeyboardTypeDefault];

    //14.设置return键的样式

    [textField0 setReturnKeyType:UIReturnKeySend];

 

 //15.设置leftView:x,y不起作用

    UIImageView *leftIv = [[UIImageView alloc]initWithFrame:CGRectMake(003232)];

    leftIv.image = [UIImage imageNamed:@"player1"];

    [textField0 setLeftView:leftIv];

    //16.设置leftView的显示模式:总是出现

    [textField0 setLeftViewMode:UITextFieldViewModeAlways];

    [leftIv release];

 

 //19.设置右侧清除按钮的样式:默认从不出现

    [textField0 setClearButtonMode:UITextFieldViewModeAlways];

    //20.设置再次编辑时,清除上次输入的内容

    [textField0 setClearsOnBeginEditing:YES];

 

    //21.设置二级键盘:可以在原键盘的顶部添加一个二级键盘。会影响键盘的高度 --> 键盘默认高度是216

    UIView *view0 = [[UIView alloc]initWithFrame:CGRectMake(0032030)];

    view0.backgroundColor = [UIColor greenColor];

    [textField0 setInputAccessoryView:view0];

    [view0 release];

    

    [self.view addSubview:textField0];

    [textField0 release];

    

    

    UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(4012024040)];

    textField1.backgroundColor = [UIColor yellowColor];

    textField1.borderStyle = UITextBorderStyleBezel;

    

    //22.设置一级键盘:键盘本身

    UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(00320216)];

    inputView.backgroundColor = [UIColor lightGrayColor];

    NSArray *array = @[@"�",@"☺️",@"�",@"�",@"�",@"�",@"�",@"�",@"�",@"�",@"�",@"�",@"�",@"�",@"�",@"�"];

    for (int i = 0; i < [array count]; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        [btn setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];

        btn.frame = CGRectMake(30 + 75*(i%4), 8 + 50*(i/4), 3030);

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

        [inputView addSubview:btn];

    }

    

  //定制键盘 不弹系统键盘

    //设置 inputView之后就不弹系统键盘了

 

    textField.inputView = inputView;

 

 

    [textField1 setInputView:inputView];

    [inputView release];

    textField1.tag = 101;

    [self.view addSubview:textField1];

    [textField1 release];

2.收键盘的三种方法

#pragma mark - UITextField收起键盘

/*

 1.点击空白处收起键盘

    1.1.1UITouch

    1.2.UIControl

 2.点击return键收起键盘

 */

 

#if 1

//1.通过UITouch点击空白处收起键盘 --> UIView的方法

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    //当textField将要开始编辑时,键盘会成为第一响应者 becomeFirstResponder --> 键盘出现,键盘不会自己收回。

    

    [self hiddenKeyBoard];

    

}

#else

-(void)createControl{

    //(0,0,320,480)

    //UIControl:事件驱动型控件,可以添加事件

    UIControl *ctl = [[UIControl alloc]initWithFrame:CGRectMake(00self.view.frame.size.width, self.view.frame.size.height)];

    [ctl addTarget:self action:@selector(controlAction) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:ctl];

    //把ctl放到最下层

    [self.view sendSubviewToBack:ctl];

    

}

-(void)controlAction{

    [self hiddenKeyBoard];

}

#endif

//收键盘

-(void)hiddenKeyBoard{

    //先获取到textField

    UITextField *tmp0 = (UITextField *)[self.view viewWithTag:100];

    UITextField *tmp1 = (UITextField *)[self.view viewWithTag:101];

    //收键盘 --> resignFirstResponder

    [tmp0 resignFirstResponder];

    [tmp1 resignFirstResponder];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

 

    [self createTextField];

    

#if 1

#else

    [self createControl];

#endif

    

}

//3.使用textField协议中的方法。通过return键收回键盘

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    //是否允许return:如果设置为NO,那么点击return键时,没有任何操作

    [self hiddenKeyBoard];

    return YES;

}

3.协议相关

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    NSLog(@"即将开始编辑时调用这个方法");

    return YES;

}

-(void)textFieldDidBeginEditing:(UITextField *)textField{

    NSLog(@"开始编辑时会调用这个方法");

    //让按钮位置上移

    UIButton *tmp = (UIButton *)[self.view viewWithTag:200];

    [UIView animateWithDuration:0.3 animations:^{

        tmp.frame = CGRectMake(10019010030);

    }];

    

}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    NSLog(@"即将结束编辑时调用该方法");

    return YES;

}

-(void)textFieldDidEndEditing:(UITextField *)textField{

    NSLog(@"编辑结束时会调用该方法");

    //让按钮下移

    UIButton *tmp = (UIButton *)[self.view viewWithTag:200];

    [UIView animateWithDuration:0.3 animations:^{

        tmp.frame = CGRectMake(10030010030);

    }];

    

}

 

-(BOOL)textFieldShouldClear:(UITextField *)textField{

    //是否允许使用清除按钮:如果返回NO,那么点击清除按钮时,不会有任何操作。

    return YES;

}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    //range:光标的位置。只有位置,没有长度

    //string:即将要显示在textfield里面的内容

    NSLog(@"range = %@,string = %@",NSStringFromRange(range),string);

    //限制密码只能是6位。

    NSLog(@"length = %u,stringlength = %u",textField.text.length,string.length);

    if (textField.text.length + string.length > 6) {

        return NO;

    }

    return YES;

}

 

 

4.通知收键盘

   self.view.backgroundColor = [UIColor orangeColor];

    //一旦界面被加载 就应该立即先注册观察者对象

    //首先获取通知中心 一个程序只有一个通知中心

    NSNotificationCenter *fc = [NSNotificationCenter defaultCenter];

    /*

     第一个参数:观察者对象的地址

     第二个参数:观察者的行为方法

     第三个参数:监听的通知的名字

     第四个参数:谁发送的通知   nil 表示可以接收任意对象发送的通知

     一个监听到通知之后  观察者self 就立即执行keyboardWillShow:

     */

    //注册观察者;

    //监听键盘弹起的通知

    [fc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    //注册一个观察者 监听 键盘收起的通知

    [fc addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];

    //观察者 一定要在发通知之前注册,一般都是在初始化的时候注册

 

 

 

 

 

//监听到键盘 弹起的时候 观察者要调用的函数

- (void)keyboardWillShow:(NSNotification *)nf {

    //获取通知的所有内容 放入一个字典中

    NSDictionary *dict = [nf userInfo];

    NSLog(@"dict:%@",dict);

    //获取弹键盘的时间

    double t = [[dict objectForKey:UIKeyboardAnimationDurationUserInfoKeydoubleValue];

    [UIView animateWithDuration:t animations:^{

        //需要产生动画的代码写在这里

        UIButton *button1 = (UIButton*)[self.view viewWithTag:201];

        UIButton *button2 = (UIButton*)[self.view viewWithTag:202];

        button1.frame = CGRectMake(6020010050);

        button2.frame = CGRectMake(16020010050);

    } completion:^(BOOL finished) {

        //动画 完成之后会执行这个block

        NSLog(@"动画完成");

    }];

 

}

//监听到键盘 收起的时候 观察者要调用的函数

- (void)keyboardWillHidden:(NSNotification *)nf {

    //动画

    [UIView animateWithDuration:0.25 animations:^{

        //需要产生动画的代码写在这里

        UIButton *button1 = (UIButton*)[self.view viewWithTag:201];

        UIButton *button2 = (UIButton*)[self.view viewWithTag:202];

        button1.frame = CGRectMake(6025010050);

        button2.frame = CGRectMake(16025010050);

    } completion:^(BOOL finished) {

        //动画 完成之后会执行这个block

        NSLog(@"动画完成");

    }];

 

}

 

 

 

 

   //dealloc 的时候 要删除观察者

    [[NSNotificationCenter defaultCenterremoveObserver:self];

0 0
原创粉丝点击