UITextField 基本使用方法總結
1.初始化textfield並設定位置及大小
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
2.設定輸入框內容的字型樣式和大小,字型顏色,對齊方式
<wbr>text.font = [UIFont fontWithName:@"Arial" size:20.0f];</wbr>
text.textColor = [UIColor redColor];
text.text = @"輸入框預設文字";
text.textAlignment = UITextAlignmentLeft;
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//垂直居中
/*
//設定圓角邊框方法二,該方法可以設定任意控制元件圓角邊框,可以定義圓角的弧度、顏色、邊框等
CALayer *layer=[txtf1 layer];
//是否設定邊框以及是否可見
[layer setMasksToBounds:YES];
//設定邊框圓角的弧度
[layer setCornerRadius:5.0];
//設定邊框線的寬
[layer setBorderWidth:1];
//設定邊框線的顏色
[layer setBorderColor:[[UIColor blackColor] CGColor]];
*/
3.設定邊框樣式,只有設定了才會顯示邊框樣式
text.borderStyle = UITextBorderStyleRounded<wbr style="color:rgb(50,62,50); font-family:Menlo; font-size:14px; text-align:left"><span style="color:rgb(50,62,50); font-family:Menlo; font-size:14px; text-align:left">Rect;</span><br></wbr>
typedef enum {
<wbr><wbr>UITextBorderStyleNone,<wbr></wbr></wbr></wbr>
<wbr><wbr>UITextBorderStyleLine,</wbr></wbr>
<wbr><wbr>UITextBorderStyleBezel,</wbr></wbr>
<wbr><wbr>UITextBorderStyleRounded<wbr>Rect <wbr></wbr></wbr></wbr></wbr>
<wbr>} UITextBorderStyle;</wbr>
4.設定輸入框的背景顏色,此時設定為白色<wbr></wbr>如果使用了自定義的背景圖片邊框會被忽略掉
text.backgroundColor = [UIColor whiteColor];
5.設定背景圖片text.background = [UIImage imageNamed:@"dd.png"];
6.設定選中背景圖片
text.disabledBackground = [UIImage imageNamed:@"cc.png"];
7.當輸入框沒有內容時,水印提示<wbr></wbr>提示內容為password
<wbr>text.placeholder = @"password";</wbr>
8.輸入框中是否有個叉號,在什麼時候顯示,用於一次性刪除輸入框中的內容
<wbr>text.clearButtonMode = UITextFieldViewModeAlway<wbr>s;</wbr></wbr>
typedef enum {
<wbr><wbr>UITextFieldViewModeNever<wbr>,<wbr>從來<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">不出現</span></wbr></wbr></wbr></wbr>
<wbr><wbr>UITextFieldViewModeWhile<wbr>Editing,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">當編輯時候出現</span></wbr></wbr></wbr></wbr>
<wbr><wbr>UITextFieldViewModeUnles<wbr>sEditing,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">除了編輯外都出現</span></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UITextFieldViewModeAlway<wbr>s <wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">一直出現</span></wbr></wbr></wbr></wbr></wbr>
} UITextFieldViewMode;
9.輸入內容為密碼樣式text.secureTextEntry = YES;
10.是否糾錯text.autocorrectionType = UITextAutocorrectionType<wbr style="color:rgb(50,62,50); font-family:Menlo; font-size:14px; text-align:left"><span style="color:rgb(50,62,50); font-family:Menlo; font-size:14px; text-align:left">No;</span></wbr>
typedef enum {
<wbr><wbr>UITextAutocorrectionType<wbr>Default,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">預設</span></wbr></wbr></wbr></wbr>
<wbr><wbr>UITextAutocorrectionType<wbr>No, <wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">不自動糾錯</span></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UITextAutocorrectionType<wbr>Yes,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">自動糾錯</span></wbr></wbr></wbr></wbr></wbr>
} UITextAutocorrectionType<wbr>;</wbr>
11.再次編輯時候清空text.clearsOnBeginEditing = YES;
12.設定鍵盤的樣式
<wbr>text.keyboardType = UIKeyboardTypeNumberPad;</wbr>
typedef enum {
<wbr><wbr>UIKeyboardTypeDefault, <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">預設鍵盤,支援所有字元</span><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UIKeyboardTypeASCIICapab<wbr>le,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">支援</span>ASCII<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的預設鍵盤</span></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UIKeyboardTypeNumbersAnd<wbr>Punctuation,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標準電話鍵盤,支援+*#字元</span></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UIKeyboardTypeURL,<wbr><wbr><wbr><wbr><wbr><wbr>URL<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">鍵盤,支援</span>.com<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">按鈕</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">只支援</span>URL<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">字元</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
UIKeyboardTypeNumberPad,<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">數字鍵盤</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
UIKeyboardTypePhonePad,<wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">電話鍵盤</span></wbr></wbr></wbr></wbr>
<wbr><wbr>UIKeyboardTypeNamePhoneP<wbr>ad, <wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">電話鍵盤,也支援輸入人名</span></wbr></wbr></wbr></wbr></wbr>
UIKeyboardTypeEmailAddre<wbr>ss, <wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">用於輸入電子</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">郵件地址的鍵盤</span><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
UIKeyboardTypeDecimalPad<wbr>, <wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">數字鍵盤</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">有數字和小數點</span></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UIKeyboardTypeTwitter,<wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">優化的鍵盤,方便輸入</span>@<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">、</span>#<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">字元</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapab<wbr>le,<wbr></wbr></wbr></wbr></wbr>
} UIKeyboardType;
13.首字母是否大寫
<wbr>text.autocapitalizationType = UITextAutocapitalization<wbr>TypeNone;</wbr></wbr>
typedef enum {
<wbr><wbr>UITextAutocapitalization<wbr>TypeNone,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">不自動大寫</span></wbr></wbr></wbr></wbr>
<wbr><wbr>UITextAutocapitalization<wbr>TypeWords,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">單詞首字母大寫</span></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UITextAutocapitalization<wbr>TypeSentences,<wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">句子的首字母大寫</span></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UITextAutocapitalization<wbr>TypeAllCharacters,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">所有字母都大寫</span></wbr></wbr></wbr></wbr>
} UITextAutocapitalization<wbr>Type;</wbr>
14.鍵盤外觀
textView.keyboardAppearance=UIKeyboardAppearanceDefa<wbr>ult<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">;</span></wbr>
typedef enum {
UIKeyboardAppearanceDefa<wbr>ult<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">,</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">預設外觀,淺灰色</span></wbr></wbr>
UIKeyboardAppearanceAler<wbr>t<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">,</span><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">深灰</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">石墨色</span></wbr></wbr></wbr></wbr></wbr></wbr>
} UIReturnKeyType;
15.return鍵變成什麼鍵
<wbr>text.returnKeyType =UIReturnKeyDone;</wbr>
typedef enum {
<wbr><wbr>UIReturnKeyDefault,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">預設</span><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">灰色按鈕,標有</span>Return</wbr></wbr></wbr></wbr>
<wbr><wbr>UIReturnKeyGo,<wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標有</span>Go<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的藍色按鈕</span></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>UIReturnKeyGoogle,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標有</span>Google<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的藍色按鈕,用語搜尋</span></wbr></wbr>
<wbr><wbr>UIReturnKeyJoin,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標有</span>Join<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的藍色按鈕</span></wbr></wbr>
<wbr><wbr>UIReturnKeyNext,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標有</span>Next<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的藍色按鈕</span></wbr></wbr>
<wbr><wbr>UIReturnKeyRoute,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標有</span>Route<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的藍色按鈕</span></wbr></wbr>
<wbr><wbr>UIReturnKeySearch,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標有</span>Search<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的藍色按鈕</span></wbr></wbr>
<wbr><wbr>UIReturnKeySend,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標有</span>Send<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的藍色按鈕</span></wbr></wbr>
<wbr><wbr>UIReturnKeyYahoo,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標有</span>Yahoo<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的藍色按鈕</span></wbr></wbr>
<wbr><wbr>UIReturnKeyYahoo,<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">標有</span>Yahoo<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">的藍色按鈕</span></wbr></wbr>
<wbr><wbr>UIReturnKeyEmergencyCall<wbr>,<wbr><span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">緊急呼叫按鈕</span></wbr></wbr></wbr></wbr>
} UIReturnKeyType;
16.設定代理,把textfield加到檢視中text.delegate = self;
[self.window addSubview:text];
17.最右側加圖片(左側類似)
<wbr><wbr>UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];</wbr></wbr>
<wbr><wbr>text.rightView=image;</wbr></wbr>
<wbr><wbr>text.rightViewMode = UITextFieldViewModeAlway<wbr>s;</wbr></wbr></wbr>
typedef enum {
<wbr><wbr>UITextFieldViewModeNever<wbr>,</wbr></wbr></wbr>
<wbr><wbr>UITextFieldViewModeWhile<wbr>Editing,</wbr></wbr></wbr>
<wbr><wbr>UITextFieldViewModeUnles<wbr>sEditing,</wbr></wbr></wbr>
<wbr><wbr>UITextFieldViewModeAlway<wbr>s</wbr></wbr></wbr>
} UITextFieldViewMode;
18.按return鍵鍵盤往下收<wbr style="color:rgb(220,32,3); font-family:Menlo; font-size:14px; text-align:left"><span style="color:rgb(220,32,3); font-family:Menlo; font-size:14px; text-align:left">becomeFirstResponder(<span style="word-wrap:normal; word-break:normal; color:rgb(220,32,3); font-size:14px; text-align:left; padding:0px; margin:0px; font-family:'Heiti sC Light'">要採用</span><span style="color:rgb(220,32,3); font-family:Menlo; font-size:14px; text-align:left">UITextFieldDelegate</span><span style="word-wrap:normal; word-break:normal; color:rgb(220,32,3); font-size:14px; text-align:left; padding:0px; margin:0px; font-family:'Heiti sC Light'">協議</span>)</span></wbr>
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
<wbr><wbr>[text resignFirstResponder];<wbr><wbr>//<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">主要是</span>[receiver resignFirstResponder]<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">在哪呼叫就能把</span>receiver<span style="word-wrap:normal; word-break:normal; padding:0px; margin:0px; font-family:'Heiti sC Light'">對應的鍵盤往下收</span></wbr></wbr></wbr></wbr>
<wbr><wbr>return YES;</wbr></wbr>
}
//補充點新內容 (從網上看到的)
//限制輸入的最大字數
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string isEqualToString:@"\n"]){
return YES;
}
NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (self.searchTextField == textField)
{
if ([aString length] > 5) {
textField.text = [aString substringToIndex:5];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"超過最大字數不能輸入了"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
return NO;
}
}
return YES;
}
一下補充內容來源於網上:
重寫繪製行為
除了UITextField物件的風格選項,你還可以定製化UITextField物件,為他新增許多不同的重寫方法,來改變文字欄位的顯示行為。這些方法都會返回一個CGRect結構,制定了文字欄位每個部件的邊界範圍。以下方法都可以重寫。
–textRectForBounds: //重寫來重置文字區域
–drawTextInRect://改變繪文字屬性.重寫時呼叫super可以按預設圖形屬性繪製,若自己完全重寫繪製函式,就不用呼叫super了.
–placeholderRectForBounds://重寫來重置佔位符區域
–drawPlaceholderInRect://重寫改變繪製佔位符屬性.重寫時呼叫super可以按預設圖形屬性繪製,若自己完全重寫繪製函式,就不用呼叫super了.
–borderRectForBounds://重寫來重置邊緣區域
–editingRectForBounds://重寫來重置編輯區域
–clearButtonRectForBounds://重寫來重置clearButton位置,改變size可能導致button的圖片失真
–leftViewRectForBounds:
–rightViewRectForBounds:
委託方法
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField{
//返回一個BOOL值,指定是否循序文字欄位開始編輯
returnYES;
}
-(void)textFieldDidBeginEditing:(UITextField*)textField{
//開始編輯時觸發,文字欄位將成為firstresponder
}
-(BOOL)textFieldShouldEndEditing:(UITextField*)textField{
//返回BOOL值,指定是否允許文字欄位結束編輯,當編輯結束,文字欄位會讓出firstresponder
//要想在使用者結束編輯時阻止文字欄位消失,可以返回NO
//這對一些文字欄位必須始終保持活躍狀態的程式很有用,比如即時訊息
returnNO;
}
-(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString*)string{
//當用戶使用自動更正功能,把輸入的文字修改為推薦的文字時,就會呼叫這個方法。
//這對於想要加入撤銷選項的應用程式特別有用
//可以跟蹤欄位內所做的最後一次修改,也可以對所有編輯做日誌記錄,用作審計用途。
//要防止文字被改變可以返回NO
//這個方法的引數中有一個NSRange物件,指明瞭被改變文字的位置,建議修改的文字也在其中
returnYES;
}
-(BOOL)textFieldShouldClear:(UITextField*)textField{
//返回一個BOOL值指明是否允許根據使用者請求清除內容
//可以設定在特定條件下才允許清除內容
returnYES;
}
-(BOOL)textFieldShouldReturn:(UITextField*)textField{
//返回一個BOOL值,指明是否允許在按下回車鍵時結束編輯
//如果允許要呼叫resignFirstResponder方法,這回導致結束編輯,而鍵盤會被收起[textFieldresignFirstResponder];
//查一下resign這個單詞的意思就明白這個方法了
returnYES;
}
通知
UITextField派生自UIControl,所以UIControl類中的通知系統在文字欄位中也可以使用。除了UIControl類的標準事件,你還可以使用下列UITextField類特有的事件
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
當文字欄位退出編輯模式時觸發。通知的object屬性儲存了最終文字。
因為文字欄位要使用鍵盤輸入文字,所以下面這些事件發生時,也會發送動作通知
UIKeyboardWillShowNotification //鍵盤顯示之前傳送
UIKeyboardDidShowNotification //鍵盤顯示之後傳送
UIKeyboardWillHideNotification //鍵盤隱藏之前傳送
UIKeyboardDidHideNotification //鍵盤隱藏之後傳送
1、Text:設定文字框的預設文字。
2、Placeholder:可以在文字框中顯示灰色的字,用於提示使用者應該在這個文字框輸入什麼內容。當這個文字框中輸入了資料時,用於提示的灰色的字將會自動消失。
3、Background:
4、Disabled:若選中此項,使用者將不能更改文字框內容。
5、接下來是三個按鈕,用來設定對齊方式。
6、Border Style:選擇邊界風格。
7、Clear Button:這是一個下拉選單,你可以選擇清除按鈕什麼時候出現,所謂清除按鈕就是出一個現在文字框右邊的小X,你可以有以下選擇:
7.1 Never appears:從不出現
7.2 Appears while editing:編輯時出現
7.3 Appears unless editing:
7.4 Is always visible:總是可見
8、Clear when editing begins:若選中此項,則當開始編輯這個文字框時,文字框中之前的內容會被清除掉。比如,你現在這個文字框A中輸入了"What",之後去編輯文字框B,若再回來編輯文字框A,則其中的"What"會被立即清除。
9、Text Color:設定文字框中文字的顏色。
10、Font:設定文字的字型與字號。
11、Min Font Size:設定文字框可以顯示的最小字型(不過我感覺沒什麼用)
12、Adjust To Fit:指定當文字框尺寸減小時,文字框中的文字是否也要縮小。選擇它,可以使得全部文字都可見,即使文字很長。但是這個選項要跟Min Font Size配合使用,文字再縮小,也不會小於設定的Min Font Size。
接下來的部分用於設定鍵盤如何顯示。
13、Captitalization:設定大寫。下拉選單中有四個選項:
13.1 None:不設定大寫
13.2 Words:每個單詞首字母大寫,這裡的單詞指的是以空格分開的字串
13.3 Sentances:每個句子的第一個字母大寫,這裡的句子是以句號加空格分開的字串
13.4 All Characters:所以字母大寫
14、Correction:檢查拼寫,預設是YES。
15、Keyboard:選擇鍵盤型別,比如全數字、字母和數字等。
16、Appearance:
17、Return Key:選擇返回鍵,可以選擇Search、Return、Done等。
18、Auto-enable Return Key:如選擇此項,則只有至少在文字框輸入一個字元後鍵盤的返回鍵才有效。
19、Secure:當你的文字框用作密碼輸入框時,可以選擇這個選項,此時,字元顯示為星號。
1.Alignment Horizontal水平對齊方式
2.Alignment Vertical垂直對齊方式
3.用於返回一個BOOL值輸入框是否Selected(選中) Enabled(可用) Highlighted(高亮)
限制只能輸入特定的字元
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSCharacterSet *cs;
cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];//按cs分離出陣列,陣列按@""分離出字串
BOOL canChange = [string isEqualToString:filtered];
return canChange;
}
上面那個NUMBERS是一個巨集,可以在檔案頂部定義:
#define NUMBERS @”0123456789\n”(這個代表可以輸入數字和換行,請注意這個\n,如果不寫這個,Done按鍵將不會觸發,如果用在SearchBar中,將會不觸發Search事件,因為你自己限制不讓輸入\n,好慘,我在專案中才發現的。)
所以,如果你要限制輸入英文和數字的話,就可以把這個定義為:
#define kAlphaNum @”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″。
當然,你還可以在以上方法return之前,做一提示的,比如提示使用者只能輸入數字之類的。如果你覺得有需要的話。
限制只能輸入一定長度的字元
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{//string就是此時輸入的那個字元textField就是此時正在輸入的那個輸入框返回YES就是可以改變輸入框的值NO相反
if ([string isEqualToString:@"\n"])//按會車可以改變
{
return YES;
}
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];//得到輸入框的內容
if (self.myTextField == textField)//判斷是否時我們想要限定的那個輸入框
{
if ([toBeString length] > 20) {//如果輸入框內容大於20則彈出警告
textField.text = [toBeString substringToIndex:20];
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:nil message:@"超過最大字數不能輸入了" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil] autorelease];
[alert show];
return NO;
}
}
return YES;
}