实现点击屏幕键盘自动隐藏和输入密码加密功能

实现点击屏幕键盘自动隐藏和输入密码加密功能

#import "ViewController.h"

@interface ViewController ()< UITexFiledDelagate >

@end

@implementation ViewController 

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITextField * tf = [[UITextField alloc]initWithFrame:CGRectMake(100, 150, 230, 45)];
    tf.borderStyle = UITextBorderStyleRoundedRect;// 设置文本框边框
    tf.clearsOnBeginEditing = YES;// 在开始编辑的时候清除上次余留的文本
    tf.tag = 101;
    tf.adjustsFontSizeToFitWidth = YES;
    //    tf.borderStyle = UITextBorderStyleBezel;
    //    tf.backgroundColor = [UIColor redColor];
    tf.placeholder = @"Please in put your name"; // 提示输入信息
    //    tf.background = [UIImage imageNamed:<#(NSString *)#>];// 添加背景图片
    [self.view addSubview:tf];
   BOOL isEditing = tf.isEditing;// 只读,是否科协
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;// 右侧清除按钮
//    tf.leftView = [];
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    view.backgroundColor = [UIColor redColor];
//    tf.inputView = view ;// 可以自定义键盘
//    tf.inputAccessoryView = view;// 键盘附加视图,可以加表情的子视图 重点
    tf.secureTextEntry = YES;// 密码模式,加密
    tf.keyboardType = UIKeyboardTypeDefault;//设置键盘类型
    tf.returnKeyType = UIReturnKeyDone;// return键名替换
    
    // 代理delegate
    tf.delegate = self;// 设置代理调用代理方法,只要设置好代理,系统就会自动调用代理方法
    // 增加事件
    [tf addTarget:self action:@selector(tfAction) forControlEvents:UIControlEventEditingDidEndOnExit];//点击return触发
//    UIControlEventEditingDidEnd 结束第一响应者时执行
//    UIControlEventEditingDidEndOnExit 点击return触发
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 设置键盘消失
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//点击屏幕触发
    UITextField * tf =(UITextField *) [self.view viewWithTag:101];
    [tf resignFirstResponder];// 放弃第一响应者,
}

#pragma mark - UITexFiledDelagate 文本框代理

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;// NO:点击文本框键盘不出来,Yes出来
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"编辑开始");// 结束编辑后执行
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES; // 当返回NO,第一响应者响应失败,YES可以响应
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"编辑结束");
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES; // 
}
//- (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
#pragma mark - TextField增加事件
- (void)tfAction{
    NSLog(@"文本框点击事件");
}
@end

你可能感兴趣的:(实现点击屏幕键盘自动隐藏和输入密码加密功能)