//初始化textfield并設置位置及大小
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 130, 30)];
typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
text.borderStyle = UITextBorderStyleRoundedRect;
//設置輸入框的背景顏色,此時設置為白色 如果使用了自定義的背景圖片邊框會被忽略掉
text.backgroundColor = [UIColor whiteColor];
//設置背景
text.background = [UIImage imageNamed:@"background"];text.disabledBackground = [UIImage imageNamed:@"disable_background"]
//當輸入框沒有內容時,水印提示 提示內容為passWord
text.placeholder = @"password";
//設置輸入框內容的字體樣式和大小
text.font = [UIFont fontWithName:@"Arial" size:20.0f];
//設置字體顏色
text.textColor = [UIColor redColor];
//輸入框中是否有個叉號,在什么時候顯示,用于一次性刪除輸入框中的內容
typedef enum {
UITextFieldViewModeNever, 從不出現
UITextFieldViewModeWhileEditing, 編輯時出現
UITextFieldViewModeUnlessEditing, 除了編輯外都出現
UITextFieldViewModeAlways 一直出現
} UITextFieldViewMode;
text.clearButtonMode = UITextFieldViewModeAlways;
//輸入框中一開始就有的文字
text.text = @"一開始就在輸入框的文字";
//每輸入一個字符就變成點 用于密碼輸入
text.secureTextEntry = YES;
//是否糾錯
typedef enum {
UITextAutocorrectionTypeDefault, 默認
UITextAutocorrectionTypeNo, 不自動糾錯
UITextAutocorrectionTypeYes, 自動糾錯
} UITextAutocorrectionType;
text.autocorrectionType = UITextAutocorrectionTypeNo;
//再次編輯就清空
text.clearsOnBeginEditing = YES;
//內容對齊方式
text.textAlignment = UITextAlignmentLeft;
//內容的垂直對齊方式 UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//設置為YES時文本會自動縮小以適應文本窗口大小.默認是保持原來大小,而讓長文本滾動
textFied.adjustsFontSizeToFitWidth = YES;
//設置自動縮小顯示的最小字體大小
text.minimumFontSize = 20;
//設置鍵盤的樣式
typedef enum {
UIKeyboardTypeDefault, 默認鍵盤,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默認鍵盤
UIKeyboardTypeNumbersAndPunctuation, 標準電話鍵盤,支持+*#字符
UIKeyboardTypeURL, URL鍵盤,支持.com按鈕 只支持URL字符
UIKeyboardTypeNumberPad, 數字鍵盤
UIKeyboardTypePhonePad, 電話鍵盤
UIKeyboardTypeNamePhonePad, 電話鍵盤,也支持輸入人名
UIKeyboardTypeEmailAddress, 用于輸入電子 郵件地址的鍵盤
UIKeyboardTypeDecimalPad, 數字鍵盤 有數字和小數點
UIKeyboardTypeTwitter, 優化的鍵盤,方便輸入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
text.keyboardType = UIKeyboardTypeNumberPad;
//首字母是否大寫
typedef enum {
UITextAutocapitalizationTypeNone, 不自動大寫
UITextAutocapitalizationTypeWords, 單詞首字母大寫
UITextAutocapitalizationTypeSentences, 句子的首字母大寫
UITextAutocapitalizationTypeAllCharacters, 所有字母都大寫
} UITextAutocapitalizationType;
text.autocapitalizationType = UITextAutocapitalizationTypeNone;
//return鍵變成什么鍵
typedef enum {
UIReturnKeyDefault, 默認 灰色按鈕,標有Return
UIReturnKeyGo, 標有Go的藍色按鈕
UIReturnKeyGoogle,標有Google的藍色按鈕,用語搜索
UIReturnKeyJoin,標有Join的藍色按鈕
UIReturnKeyNext,標有Next的藍色按鈕
UIReturnKeyRoute,標有Route的藍色按鈕
UIReturnKeySearch,標有Search的藍色按鈕
UIReturnKeySend,標有Send的藍色按鈕
UIReturnKeyYahoo,標有Yahoo的藍色按鈕
UIReturnKeyYahoo,標有Yahoo的藍色按鈕
UIReturnKeyEmergencyCall, 緊急呼叫按鈕
} UIReturnKeyType;
text.returnKeyType = UIReturnKeyDone;
//鍵盤外觀
typedef enum {
UIKeyboardAppearanceDefault, 默認外觀,淺灰色
UIKeyboardAppearanceAlert, 深灰 石墨色
} UIReturnKeyType;
text.keyboardAppearance = UIKeyboardAppearanceDefault;
//最右側加圖片 左側類似
typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right"]]; text.rightView = image; text.rightViewMode = UITextFieldViewModeAlways;
//設置代理 用于實現協議
text.delegate = self;
//按return鍵鍵盤往下收
//聲明text的代理是我,類要采用UITextFieldDelegate協議
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [text resignFirstResponder]; //主要是[receiver resignFirstResponder]在哪調用就能把receiver對應的鍵盤往下收 return YES;}
//返回一個BOOL值,指定是否循序文本字段開始編輯
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ return YES; }
//開始編輯時觸發,文本字段將成為first responder
- (void)textFieldDidBeginEditing:(UITextField *)textField{ }
//字段結束編輯,當編輯結束,文本字段會讓出first responder,對一些文本字段必須始終保持活躍狀態的程序很有用,比如即時消息
//要想在用戶結束編輯時阻止文本字段消失,可以返回NO
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ return NO; }
//當用戶使用自動更正功能,把輸入的文字修改為推薦的文字時,就會調用這個方法。
//這對于想要加入撤銷選項的應用程序特別有用
//可以跟蹤字段內所做的最后一次修改,也可以對所有編輯做日志記錄,用作審計用途。
//要防止文字被改變可以返回NO
//這個方法的參數中有一個NSRange對象,指明了被改變文字的位置,建議修改的文本也在其中
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ return YES; }
//返回一個BOOL值指明是否允許根據用戶請求清除內容
//可以設置在特定條件下才允許清除內容
- (BOOL)textFieldShouldClear:(UITextField *)textField{ return YES; }
//返回一個BOOL值,指明是否允許在按下回車鍵時結束編輯
//如果允許要調用resignFirstResponder 方法,這回導致結束編輯,而鍵盤會被收起[textField resignFirstResponder];
//查一下resign這個單詞的意思就明白這個方法了
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ return YES; }
//限制只能輸入一定長度的字符
- (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; }
//限制只能輸入特定的字符
//#define NUMBERS @”0123456789/n” (這個代表可以輸入數字和換行,請注意這個/n,如果不寫這個,Done按鍵將不會觸發,如果用在SearchBar中,將會不觸發Search事件,因為你自己限制不讓輸入/n,好慘,我在項目中才發現的。)
//所以,如果你要限制輸入英文和數字的話,就可以把這個定義為:
//#define kAlphaNum @”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789&PRime;。
//當然,你還可以在以上方法return之前,做一提示的,比如提示用戶只能輸入數字之類的。如果你覺得有需要的話。
#define kNUMBERS @”0123456789/n”- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSCharacterSet *cs; cs = [[NSCharacterSet characterSetWithCharactersInString:kNUMBERS]invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分離出數組,數組按@""分離出字符串 BOOL canChange = [string isEqualToString:filtered]; return canChange;}
//UITextField派生自UIControl,所以UIControl類中的通知系統在文本字段中也可以使用。除了UIControl類的標準事件,你還可以使用下列UITextField類特有的事件
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
//當文本字段退出編輯模式時觸發。通知的object屬性存儲了最終文本。
//因為文本字段要使用鍵盤輸入文字,所以下面這些事件發生時,也會發送動作通知
UIKeyboardWillShowNotification //鍵盤顯示之前發送
UIKeyboardDidShowNotification //鍵盤顯示之后發送
UIKeyboardWillHideNotification //鍵盤隱藏之前發送
UIKeyboardDidHideNotification //鍵盤隱藏之后發送
新聞熱點
疑難解答