天天看點

UIKeyboard鍵盤相關知識點-IOS開發

一、鍵盤風格   

UIKit架構支援8種風格鍵盤。

typedef enum {  

    UIKeyboardTypeDefault,                // 預設鍵盤:支援所有字元  

    UIKeyboardTypeASCIICapable,           // 支援ASCII的預設鍵盤  

    UIKeyboardTypeNumbersAndPunctuation,  // 标準電話鍵盤,支援+*#等符号  

    UIKeyboardTypeURL,                    // URL鍵盤,有.com按鈕;隻支援URL字元  

    UIKeyboardTypeNumberPad,              //數字鍵盤  

    UIKeyboardTypePhonePad,               // 電話鍵盤  

    UIKeyboardTypeNamePhonePad,           // 電話鍵盤,也支援輸入人名字  

    UIKeyboardTypeEmailAddress,           // 用于輸入電子郵件位址的鍵盤  

} UIKeyboardType;  

用法用例:

textView.keyboardtype = UIKeyboardTypeNumberPad;

二、鍵盤外觀

    UIKeyboardAppearanceDefault,    // 預設外觀:淺灰色  

    UIKeyboardAppearanceAlert,      //深灰/石墨色  

} UIKeyboardAppearance;  

textView.keyboardAppearance=UIKeyboardAppearanceDefault;

三、Enter鍵

    UIReturnKeyDefault,  //預設:灰色按鈕,标有Return

    UIReturnKeyGo,  //标有Go的藍色按鈕

    UIReturnKeyGoogle,  //标有Google的藍色按鈕,用于搜尋

    UIReturnKeyJoin,  //标有Join的藍色按鈕

    UIReturnKeyNext,  //标有Next的藍色按鈕

    UIReturnKeyRoute,  //标有Route的藍色按鈕

    UIReturnKeySearch,  //标有Search的藍色按鈕

    UIReturnKeySend,  //标有Send的藍色按鈕

    UIReturnKeyYahoo,  //标有Yahoo!的藍色按鈕,用于搜尋

    UIReturnKeyDone,  //标有Done的藍色按鈕

    UIReturnKeyEmergencyCall,  //緊急呼叫按鈕

} UIReturnKeyType;  

textView.returnKeyType=UIReturnKeyGo;

四、自動大寫

    UITextAutocapitalizationTypeNone, //不自動大寫  

    UITextAutocapitalizationTypeWords, //單詞首字母大寫  

    UITextAutocapitalizationTypeSentences, //句子首字母大寫  

    UITextAutocapitalizationTypeAllCharacters, //所有字母大寫  

} UITextAutocapitalizationType;  

textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

五、自動更正

    UITextAutocorrectionTypeDefault,//預設  

    UITextAutocorrectionTypeNo,//不自動更正  

    UITextAutocorrectionTypeYes,//自動更正  

} UITextAutocorrectionType;  

textField.autocorrectionType = UITextAutocorrectionTypeYes;

六、安全文本輸入

textView.secureTextEntry=YES;

開啟安全輸入主要是用于密碼或一些私人資料的輸入,此時會禁用自動更正和自此緩存。

七、鍵盤遮住視圖

預設情況下打開鍵盤會遮住下面的view,帶來一點點困擾,不過這不是什麼大問題,我們使用點小小的手段就可以解決。

首先我們要知道鍵盤的高度是固定不變的,不過在IOS 5.0 以後鍵盤的高度貌似不是216了,不過不要緊,我們調整調整就是了:

iPhone

ipad

豎屏(portrait)

216

264

橫屏(landScape)

140

352

我們采取的方法就是在textField(有可能是其他控件)接收到彈出鍵盤事件時把self.view整體上移216px了(我們就以iPhone豎屏為例了)。

首先我們要設定textField的代理,我們就設為目前控制器了。

textField,delegate=self;

然後我們在目前控制器實作下面三個委托方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField  

{ //當點觸textField内部,開始編輯都會調用這個方法。textField将成為first responder   

       NSTimeInterval animationDuration = 0.30f;      

      CGRect frame = self.view.frame;  

      frame.origin.y -=216;  

      frame.size.height +=216;  

      self.view.frame = frame;  

       [UIView beginAnimations:@"ResizeView" context:nil];  

       [UIView setAnimationDuration:animationDuration];  

       self.view.frame = frame;                  

       [UIView commitAnimations];                  

}  

- (BOOL)textFieldShouldReturn:(UITextField *)textField   

{//當使用者按下ruturn,把焦點從textField移開那麼鍵盤就會消失了  

        NSTimeInterval animationDuration = 0.30f;  

        CGRect frame = self.view.frame;      

        frame.origin.y +=216;        

        frame.size. height -=216;     

        self.view.frame = frame;  

    //self.view移回原位置    

    [UIView beginAnimations:@"ResizeView" context:nil];  

    [UIView setAnimationDuration:animationDuration];  

        self.view.frame = frame;                  

        [UIView commitAnimations];  

        [textField resignFirstResponder];     

}         

  本文轉自新風作浪 51CTO部落格,原文連結:http://blog.51cto.com/duxinfeng/1208763,如需轉載請自行聯系原作者