實現點擊屏幕鍵盤自動隱藏和輸入密碼加密功能
#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觸發(fā)
// UIControlEventEditingDidEnd 結束第一響應者時執(zhí)行
// UIControlEventEditingDidEndOnExit 點擊return觸發(fā)
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 設置鍵盤消失
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//點擊屏幕觸發(fā)
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(@"編輯開始");// 結束編輯后執(zhí)行
}
- (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