一、鍵盤風格 ??
UIKit框架支持8種風格鍵盤。
UIKeyboardTypeDefault:
UIKeyboardTypeASCIICapable:
UIKeyboardTypeNumbersAndPunctuation:
UIKeyboardTypeURL:
UIKeyboardTypeNumberPad:
UIKeyboardTypePhonePad:
UIKeyboardTypeNamePhonePad:
UIKeyboardTypeEmailAddress:
UIKeyboardTypeDecimalPad:
UIKeyboardTypeTwitter:
UIKeyboardTypeWebSearch:
UIKeyboardTypeAlphabet:
用法用例:
textView.keyboardtype =?UIKeyboardTypeNumberPad;
二、鍵盤外觀
用法用例:
textView.keyboardAppearance=UIKeyboardAppearanceDefault;
三、回車鍵
- typedef?enum?{??
- ????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;
四、自動大寫
用法用例:textField.autocapitalizationType?=?UITextAutocapitalizationTypeWords;
五、自動更正
用法用例: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];?????
- } ? ? ? ??