UITextField 控件和代理协议方法


//*******************1.创建:*************************

UITextField *tf = [[UITextField alloc]init];

        tf.frame = CGRectMake(40, 150, 250, 40);

        tf.backgroundColor = [UIColor lightGrayColor];

//*****************2.方法属性:***********************    

    //设置边框样式

     //    UITextBorderStyleNone,        --无

//    UITextBorderStyleLine,        --线框

//    UITextBorderStyleBezel,       --阴影

//    UITextBorderStyleRoundedRect  --圆角

    tf.borderStyle = UITextBorderStyleRoundedRect;

    

    //设置密文输入

    tf.secureTextEntry = YES;

    

    //设置提示(占位符)

    tf.placeholder = @"请输入密码";

    

    //设置清除按钮模式

//    UITextFieldViewModeNever,               --从不显示

//    UITextFieldViewModeWhileEditing,        --正在编辑的时候显示

//    UITextFieldViewModeUnlessEditing,       --非编辑时候显示

//    UITextFieldViewModeAlways               --总是显示

    tf.clearButtonMode = UITextFieldViewModeUnlessEditing;

    

    //设置字体

    tf.font = [UIFont systemFontOfSize:26];

    

    //显示文字颜色

    tf.textColor = [UIColor redColor];

    

    //设置键盘显示样式

//    UIKeyboardTypeDefault,                // Default type for the current input method.

//    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active

//    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.

//    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).

//    UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.

//    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).

//    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.

//    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).

//    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.

//    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)

//    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).

    tf.keyboardType = UIKeyboardTypePhonePad;

    

    //设置键盘的外观

//    UIKeyboardAppearanceDefault,          // Default apperance for the current input method.

//    UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),

//    UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),

    tf.keyboardAppearance = UIKeyboardAppearanceDark;


    

    //设置自动调整字体大小来适应宽度(但是有最小极限,过了还是会往后移)

    tf2.adjustsFontSizeToFitWidth = YES;

    

    //调整能显示的最小字体

    tf2.minimumFontSize = 20;

    

    //设置或者获取文本框的内容

    tf2.text= @"我是文本输入框";

    NSString *text = tf2.text;

    

    //设置文本对齐方式

    tf2.textAlignment = NSTextAlignmentCenter;

    

    //设置是否在开始编辑时清空内容

    tf2.clearsOnBeginEditing = YES;

    

    //判断当前是否在编辑状态

    //tf2.isEditing;

    

    //设置文本输入框的左视图

    UIView *view = [[UIView alloc]init];

    //设置左视图的时候X,Y坐标无效,高度在tf2文本输入框的范围内有效

    view.frame = CGRectMake(100, 100, 100, 100);

    view.backgroundColor = [UIColor purpleColor];

    tf2.leftView = view;

    //想要显示左视图必须设置视图的显示模式

    tf2.leftViewMode = UITextFieldViewModeAlways;

    

    //设置文本输入框的右视图

    UIView *view1 = [[UIView alloc]init];

    //设置左视图的时候X,Y坐标无效,高度在tf2文本输入框的范围内有效

    view1.frame = CGRectMake(100, 100, 20, 20);

    view1.backgroundColor = [UIColor orangeColor];

    tf2.rightView = view1;

    //想要显示左视图必须设置视图的显示模式

    tf2.rightViewMode = UITextFieldViewModeAlways;

    

    //设置文本输入框的右视图

    UIView *inputView = [[UIView alloc]init];

    //设置左视图的时候X,Y坐标无效,高度在tf2文本输入框的范围内有效

    inputView.frame = CGRectMake(100, 100, 100, 100);

    inputView.backgroundColor = [UIColor orangeColor];

    

    //设置响应键盘的来源(自定义软键盘)

    //tf2.inputView = inputView;

    

       //添加附属视图(在软键盘上面多显示一层内容)(二级视图)

    UIView *inputView2 = [[UIView alloc]init];

    inputView2.frame = CGRectMake(100, 100, 100, 100);

    inputView2.backgroundColor = [UIColor brownColor];

   tf2.inputAccessoryView = inputView2;


    //设置返回按钮的类型

    tf2.returnKeyType = UIReturnKeyGo;

 

//***********************************3. 为这个文本框控件设置代理***********************************

//这个控制器必须遵守协议 .h: <UIXXXXXXDalegate>

       tf.delegate = self;



//***********************************4. 调用代理的方法实现对应功能***********************************

#pragma mark - UITextFieldDelegate方法


/**

 *  这个方法返回是否可以开始编辑,当用户点击textfield的时候调用这个方法

 *

 *  @param textField 当前被点击的文本输入框对象

 *

 *  @return YES代表可以编辑,NO则不能编辑

 */

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    

    NSLog(@"%@",NSStringFromSelector(_cmd));

    

    return YES;

}


/**

 *  这个方法在开始编辑时调用(进入编辑状态时执行)

 *

 *  @param textField 当前被点击的文本输入框对象

 */

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    NSLog(@"%@",NSStringFromSelector(_cmd));

    if (textField.tag == 102) {

        NSLog(@"tf2");

    }

}


/**

 *  询问是否可以结束编辑状态(textField想要结束编辑状态时调用)

 *

 *  @param textField 当前被点击的文本输入框对象

 *

 *  @return YES:可以结束 NO:不能结束

 */

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

    return YES;

}


/**

 *  已经结束编辑状态调用该方法

 *

 *  @param textField 当前编辑文本框对象

 */

- (void)textFieldDidEndEditing:(UITextField *)textField

{

    NSLog(@"已经结束");

}


//*****************************键盘收回方法********************************


    //监听键盘弹出的通知

    //当软键盘弹出前,系统会自动发送通知信息

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) nameUIKeyboardWillShowNotification object:nil];

    

    //监听键盘收回的通知

    //当软键盘收回前,系统会自动发送通知信息

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];

/**

 *  处理键盘收回前通知事件

 *

 *  @param not <#not description#>

 */

- (void)keyboardWillHide:(NSNotification *)not

{

    //1.取到两个按钮

    for (int i=0; i<2; i++) {

        UIButton *btn = (UIButton *)[self.view viewWithTag:1000+i];

        CGRect rect = btn.frame;

        rect.origin.y += 100;

        //调用动画效果

        if ([[[UIDevice currentDevicesystemVersionfloatValue] < 8) {

            

        }else{

            

            [UIView animateWithDuration:1 animations:^{

                btn.frame = rect;

            }];

        }

        

        

        

    }

    

}


/**

 *  处理键盘弹出前通知事件

 *

 *  @param not 接收返回的通知参数

 */

- (void)keyboardWillShow:(NSNotification *)not

{

    //1.取到两个按钮

    for (int i=0; i<2; i++) {

        UIButton *btn = (UIButton *)[self.view viewWithTag:1000+i];

        CGRect rect = btn.frame;

        rect.origin.y -= 100;

        //调用动画效果

        if ([[[UIDevice currentDevicesystemVersionfloatValue] < 8) {

            

        }else{

            

            [UIView animateWithDuration:1 animations:^{

                btn.frame = rect;

            }];

        }

            



    }

}

//创建提示标签

- (void)createLabels

{

    NSArray *array = @[@"Q Q :",@"密码 :"];

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

        UILabel *label = [[UILabel alloc]init];

        label.frame = CGRectMake(40, 100 + i*150, 80, 40);

        label.text = array[i];

        [self.view addSubview:label];

    }

    

}


//创建

- (void)createField1

{

    UITextField *tf1 = [[UITextField alloc]init];

    tf1.frame = CGRectMake(130, 100, 200, 40);

    tf1.borderStyle = UITextBorderStyleRoundedRect;

    tf1.keyboardType = UIKeyboardTypeNumberPad;

    tf1.font = [UIFont systemFontOfSize:26];

    tf1.placeholder = @"请输入账号";

    tf1.delegate = self;

    tf1.tag = 101;

    tf1.clearButtonMode = UITextFieldViewModeAlways;

    

    [self.view addSubview:tf1];

}


//创建

- (void)createField2

{

    UITextField *tf2 = [[UITextField alloc]init];

    tf2.frame = CGRectMake(130, 250, 200, 40);

    tf2.borderStyle = UITextBorderStyleRoundedRect;

    //tf2.keyboardType = UIKeyboardTypeNumberPad;

    tf2.font = [UIFont systemFontOfSize:26];

    tf2.placeholder = @"请输入密码";

    tf2.secureTextEntry = YES;

    tf2.delegate = self;

    tf2.tag = 102;

    tf2.returnKeyType = UIReturnKeyGo;

    tf2.clearButtonMode = UITextFieldViewModeAlways;

    

    [self.view addSubview:tf2];

}


- (void)createBtn

{

    NSArray *arr = @[@"登 录",@"注 册"];

    for (int i=0; i<arr.count; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(80 + i*100, 500, 100, 40);

        [btn setTitle:arr[i] forState:UIControlStateNormal];

        btn.titleLabel.font = [UIFont systemFontOfSize:26];

        btn.tag = 1000 + i;

        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

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

        [self.view addSubview:btn];

    }

}


- (void)btnClick:(UIButton *)btn

{


}


/**

 *  收回软键盘方法4:

 *  从UIControl继承可以创建自定义的视图

 */

- (void)createController

{

    UIControl *ctrl = [[UIControl alloc]init];

    ctrl.frame = [[UIScreen mainScreen]bounds];

    

    [ctrl addTarget:self action:@selector(ctrlClicked) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:ctrl];

    [self.view sendSubviewToBack:ctrl];

    

}


/**

 *  ctrl点击事件,去除所有空间第一响应者

 */

- (void)ctrlClicked

{

    [self.view endEditing:YES];

}


#pragma mark - 触摸相关方法


/**

 *  收回软键盘的方式2

 *

 *  @param touches <#touches description#>

 *  @param event   <#event description#>

 */

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

{

    //让所有的文本输入框成为第一响应者

//    for (int i=0; i<2; i++) {

//        UITextField *tf = (UITextField *)[self.view viewWithTag:101+i];

//        [tf resignFirstResponder];

//    }

    

    //方法3:让VIEW上所有的子视图结束编辑(取消成为第一响应者)

    [self.view endEditing:YES];

    


}


#pragma mark - UITextFieldDelegate方法


/**

 *  当用户点击清楚按钮时调用

 *

 *  @param textField <#textField description#>

 *

 *  @return YES:可以清除 NO:不能清除

 */

- (BOOL)textFieldShouldClear:(UITextField *)textField

{

    if (textField.tag == 101) {

        //清除按钮内容

        //textField.text = @"";

        return NO;

    }else{

        return YES;

    }

}


/**

 *  设置返回键是否可以返回(常在这里设置收回软键盘),当用户点击返回按钮时被调用

 *

 *  @param textField 当前被点击的文本输入框对象

 *

 *  @return YES:可以返回了 NO:不可以返回

 */

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    //收回软键盘的方式1:textField会自动退出编辑模式,并且收回软键盘

    [textField resignFirstResponder];

    

    

    return YES;

}


/**

 *  这个方法在用户点击软键盘,textField将用哦过户输入数据放入textField之前调用

 *  (可以在这里处理用户输入的信息做出判断)

 *  @param textField 当前被点击的文本输入框对象

 *  @param range     需要替换的字符串范围

 *  @param string    用户输入的字符

 *

 *  @return YES:用户输入内容添加到textField中,NO:不添加

 */

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

{

    if ([string isEqualToString:@"0"]) {

        return NO;

    }

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

    return  YES;

    

}

/**

 *  这个方法返回是否可以开始编辑,当用户点击textfield的时候调用这个方法

 *

 *  @param textField 当前被点击的文本输入框对象

 *

 *  @return YES代表可以编辑,并且系统让这个textField成为第一响应者,进入编辑状态,并且弹出软键盘;NO则不能编辑

 */

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    return YES;

}


/**

 *  这个方法在开始编辑时调用(进入编辑状态时执行)

 *

 *  @param textField 当前被点击的文本输入框对象

 */

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    NSLog(@"%@",NSStringFromSelector(_cmd));

    if (textField.tag == 102) {

        NSLog(@"tf2");

    }

}


/**

 *  询问是否可以结束编辑状态(textField想要结束编辑状态时调用)

 *

 *  @param textField 当前被点击的文本输入框对象

 *

 *  @return YES:可以结束 NO:不能结束

 */

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

    return YES;

}


/**

 *  已经结束编辑状态调用该方法

 *

 *  @param textField 当前编辑文本框对象

 */

- (void)textFieldDidEndEditing:(UITextField *)textField

{

    NSLog(@"已经结束");

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值