//初始化textfield並設置位置及大小less
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
//設置邊框樣式,只有設置了纔會顯示邊框樣式 字體
text.borderStyle = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone, UITextBorderStyleLine, UITextBorderStyleBezel, UITextBorderStyleRoundedRect } UITextBorderStyle;
//設置輸入框的背景顏色,此時設置爲白色 若是使用了自定義的背景圖片邊框會被忽略掉 優化
text.backgroundColor = [UIColor whiteColor];
//設置背景spa
text.background = [UIImage imageNamed:@"dd.png"];
//設置背景 3d
text.disabledBackground = [UIImage imageNamed:@"cc.png"];
//當輸入框沒有內容時,水印(佔位符)提示 提示內容爲password代理
text.placeholder = @"password";
//設置水印字的顏色,水印的字體顏色默認爲灰色。如下代碼是給水印字體改顏色。code
[text setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//設置輸入框內容的字體樣式和大小orm
text.font = [UIFont fontWithName:@"Arial" size:20.0f];
//設置字體顏色繼承
text.textColor = [UIColor redColor];
//輸入框中是否有個叉號,在何時顯示,用於一次性刪除輸入框中的內容圖片
text.clearButtonMode = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, 重不出現 UITextFieldViewModeWhileEditing, 編輯時出現 UITextFieldViewModeUnlessEditing, 除了編輯外都出現 UITextFieldViewModeAlways 一直出現 } UITextFieldViewMode;
//每輸入一個字符就變成點 用語密碼輸入
text.secureTextEntry = YES;
//是否糾錯
text.autocorrectionType = UITextAutocorrectionTypeNo; typedef enum { UITextAutocorrectionTypeDefault, 默認 UITextAutocorrectionTypeNo, 不自動糾錯 UITextAutocorrectionTypeYes, 自動糾錯 } UITextAutocorrectionType;
//再次編輯就清空
text.clearsOnBeginEditing = YES;
//內容對齊方式
text.textAlignment = UITextAlignmentLeft;
//內容的垂直對齊方式 UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//設置爲YES時文本會自動縮小以適應文本窗口大小.默認是保持原來大小,而讓長文本滾動
textFied.adjustsFontSizeToFitWidth = YES;
//設置自動縮小顯示的最小字體大小
text.minimumFontSize = 20;
//設置鍵盤的樣式
text.keyboardType = UIKeyboardTypeNumberPad; typedef enum { UIKeyboardTypeDefault, 默認鍵盤,支持全部字符 UIKeyboardTypeASCIICapable, 支持ASCII的默認鍵盤 UIKeyboardTypeNumbersAndPunctuation, 標準電話鍵盤,支持+*#字符 UIKeyboardTypeURL, URL鍵盤,支持.com按鈕 只支持URL字符 UIKeyboardTypeNumberPad, 數字鍵盤 UIKeyboardTypePhonePad, 電話鍵盤 UIKeyboardTypeNamePhonePad, 電話鍵盤,也支持輸入人名 UIKeyboardTypeEmailAddress, 用於輸入電子 郵件地址的鍵盤 UIKeyboardTypeDecimalPad, 數字鍵盤 有數字和小數點 UIKeyboardTypeTwitter, 優化的鍵盤,方便輸入@、#字符 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, } UIKeyboardType;
//return鍵變成什麼鍵
text.returnKeyType =UIReturnKeyDone; typedef enum { UIReturnKeyDefault, 默認 灰色按鈕,標有Return UIReturnKeyGo, 標有Go的藍色按鈕 UIReturnKeyGoogle,標有Google的藍色按鈕,用語搜索 UIReturnKeyJoin,標有Join的藍色按鈕 UIReturnKeyNext,標有Next的藍色按鈕 UIReturnKeyRoute,標有Route的藍色按鈕 UIReturnKeySearch,標有Search的藍色按鈕 UIReturnKeySend,標有Send的藍色按鈕 UIReturnKeyYahoo,標有Yahoo的藍色按鈕 UIReturnKeyYahoo,標有Yahoo的藍色按鈕 UIReturnKeyEmergencyCall, 緊急呼叫按鈕 } UIReturnKeyType;
//鍵盤外觀
textView.keyboardAppearance=UIKeyboardAppearanceDefault; typedef enum { UIKeyboardAppearanceDefault, 默認外觀,淺灰色 UIKeyboardAppearanceAlert, 深灰 石墨色 } UIReturnKeyType;
//設置代理 用於實現協議
text.delegate = self;
//把textfield加到視圖中
[self.window addSubview:text];
//最右側加圖片是如下代碼 左側相似
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]]; text.rightView=image; text.rightViewMode = UITextFieldViewModeAlways; typedef enum { UITextFieldViewModeNever, UITextFieldViewModeWhileEditing, UITextFieldViewModeUnlessEditing, UITextFieldViewModeAlways } UITextFieldViewMode;
//按return鍵鍵盤往下收 becomeFirstResponder
類要採用UITextFieldDelegate協議
text.delegate = self; 聲明text的代理是我,我會去實現把鍵盤往下收的方法 這個方法在UITextFieldDelegate裏因此咱們要採用UITextFieldDelegate這個協議
//點擊其餘任何地方鍵盤往下收[text resignFirstResponder](輸入框裏面除外);
- (BOOL)textFieldShouldReturn:(UITextField *)textField { [text resignFirstResponder]; //主要是[receiver resignFirstResponder]在哪調用就能把receiver對應的鍵盤往下收 return YES; }
//限制文本輸入的長度
[_AppView.AppoAgain addTarget:self action:@selector(AppoAgaintextFieldDidChange) forControlEvents:UIControlEventEditingChanged]; //在監聽的方法裏截取他的長度就OK了。我這裏是截取10的長度(只能輸入10位) -(void)AppoAgaintextFieldDidChange{ if(_AppView.AppoAgain.text.length >= 10){ NSString *textStr = _AppView.AppoAgain.text; _AppView.AppoAgain.text = [textStr substringToIndex:10]; } }