轉(zhuǎn)自:鍵盤的相關設置(UITextfield)
一、鍵盤風格
UIKit框架支持8種風格鍵盤。
typedef enum {
UIKeyboardTypeDefault,? ? ? ? ? ? ? ? // 默認鍵盤:支持所有字符
UIKeyboardTypeASCIICapable,? ? ? ? ? // 支持ASCII的默認鍵盤? ? 不能輸入中文
UIKeyboardTypeNumbersAndPunctuation,? // 標準電話鍵盤,支持+*#等符號
UIKeyboardTypeURL,? ? ? ? ? ? ? ? ? ? // URL鍵盤,有.com按鈕;只支持URL字符
UIKeyboardTypeNumberPad,? ? ? ? ? ? ? //數(shù)字鍵盤
UIKeyboardTypePhonePad,? ? ? ? ? ? ? // 電話鍵盤
UIKeyboardTypeNamePhonePad,? ? ? ? ? // 電話鍵盤,也支持輸入人名字? 不能輸入標點
UIKeyboardTypeEmailAddress,? ? ? ? ? // 用于輸入電子郵件地址的鍵盤
} UIKeyboardType;
用法用例:
textView.keyboardtype = UIKeyboardTypeNumberPad;
二、鍵盤外觀
typedef enum {
UIKeyboardAppearanceDefault,? ? // 默認外觀:淺灰色
UIKeyboardAppearanceAlert,? ? ? //深灰/石墨色
} UIKeyboardAppearance;
用法用例:
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;
四、自動大寫
typedef enum {
UITextAutocapitalizationTypeNone, //不自動大寫
UITextAutocapitalizationTypeWords, //單詞首字母大寫
UITextAutocapitalizationTypeSentences, //句子首字母大寫
UITextAutocapitalizationTypeAllCharacters, //所有字母大寫
} UITextAutocapitalizationType;
用法用例:
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
五、自動更正
這個是是否開啟鍵盤聯(lián)想功能;
typedef enum {
UITextAutocorrectionTypeDefault,//默認
UITextAutocorrectionTypeNo,//不自動更正
UITextAutocorrectionTypeYes,//自動更正
} UITextAutocorrectionType;
用法用例:
textField.autocorrectionType = UITextAutocorrectionTypeYes;
六、安全文本輸入
textView.secureTextEntry=YES;
開啟安全輸入主要是用于密碼或一些私人數(shù)據(jù)的輸入,此時會禁用自動更正和自此緩存。
統(tǒng)計字符和響應RETURN鍵
統(tǒng)計字符:
1、UITextView
- (void)textViewDidChange:(UITextView *)textView{
int count = [textView.text length]; //這里的count就是字符個數(shù)了
}
2、UITextField
方法一:
自己先為UITextField的Editing Changed事件添加一個響應方法
-(IBAction)valuechange//m_TextField是UITextField的一個IBOutlet
{ int count = [m_TextField.text length]; //count就是當前的字符個數(shù)
//下邊是將字符限制在140以內(nèi)
if ([m_TextField.text length]>140) { [m_TextField setText:[m_TextField.text substringToIndex:140]];//多出140時,只取前140個字符
}}
方法二:
在代理方法:- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string,判斷range.length的值來判斷輸入的是回格還是其它字符
響應Return鍵:
1、UITextView
//代理方法
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if (1 == range.length) {//按下回格鍵
return YES; } if ([text isEqualToString:@"\n"]) {//按下return鍵 //這里隱藏鍵盤,不做任何處理
[textView resignFirstResponder]; return NO; }else { if ([textView.text length] < 140) {//判斷字符個數(shù)
return YES; } } return NO;}
2、UITextField
這個直接有代理方法哈
- (BOOL)textFieldShouldReturn:(UITextField *)textField
UITextField進入編輯狀態(tài) 獲得焦點 becomeFirstResponder
關閉鍵盤 resignFirstResponder
********
---? 隱藏鍵盤彈出,但是有光標:
textField.inputView=[[UIView alloc]initWithFrame:CGRectZero];