實現(xiàn)點擊屏幕鍵盤自動隱藏和輸入密碼加密功能
#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;// 設(shè)置文本框邊框
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;// 只讀,是否科協(xié)
tf.clearButtonMode = UITextFieldViewModeWhileEditing;// 右側(cè)清除按鈕
// 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;//設(shè)置鍵盤類型
tf.returnKeyType = UIReturnKeyDone;// return鍵名替換
// 代理delegate
tf.delegate = self;// 設(shè)置代理調(diào)用代理方法,只要設(shè)置好代理,系統(tǒng)就會自動調(diào)用代理方法
// 增加事件
[tf addTarget:self action:@selector(tfAction) forControlEvents:UIControlEventEditingDidEndOnExit];//點擊return觸發(fā)
// UIControlEventEditingDidEnd 結(jié)束第一響應(yīng)者時執(zhí)行
// UIControlEventEditingDidEndOnExit 點擊return觸發(fā)
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 設(shè)置鍵盤消失
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//點擊屏幕觸發(fā)
UITextField * tf =(UITextField *) [self.view viewWithTag:101];
[tf resignFirstResponder];// 放棄第一響應(yīng)者,
}
#pragma mark - UITexFiledDelagate 文本框代理
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;// NO:點擊文本框鍵盤不出來,Yes出來
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"編輯開始");// 結(jié)束編輯后執(zhí)行
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES; // 當(dāng)返回NO,第一響應(yīng)者響應(yīng)失敗,YES可以響應(yīng)
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"編輯結(jié)束");
}
- (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
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。