實現(xiàn)點擊屏幕鍵盤自動隱藏和輸入密碼加密功能

實現(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ù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,327評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,045評論 25 709
  • 版權(quán)歸作者所有,任何形式轉(zhuǎn)載請聯(lián)系作者。 作者:葉紫(來自豆瓣) 前天晚上老師打電話讓寫二篇家庭教育的分享,也是對...
    084a4f46f700閱讀 441評論 0 1
  • 人類的繁衍生息,住所從最初的山洞、茅草屋、木屋到瓦房、高樓,不斷在變化著。 瓦房,是中國傳統(tǒng)的民居建筑,體現(xiàn)一種素...
    曲聲和閱讀 681評論 11 11
  • 孟子認為,一個真正的人,必須要有獨立的人格。不只在功名利祿面前要有,即使面對生死考驗,也要堅守氣節(jié),保持人格。不茍...
    易清塵閱讀 252評論 0 0

友情鏈接更多精彩內(nèi)容