1 //初始化textfield並設置位置及大小 2 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; 3 4 //設置邊框樣式,只有設置了纔會顯示邊框樣式 5 text.borderStyle = UITextBorderStyleRoundedRect; 6 7 typedef enum { 8 UITextBorderStyleNone, 9 UITextBorderStyleLine, 10 UITextBorderStyleBezel, 11 UITextBorderStyleRoundedRect 12 } UITextBorderStyle; 13 14 //設置輸入框的背景顏色,此時設置爲白色 若是使用了自定義的背景圖片邊框會被忽略掉 15 text.backgroundColor = [UIColor whiteColor]; 16 17 //設置背景 18 text.background = [UIImage imageNamed:@"dd.png"]; 19 20 //設置背景 21 text.disabledBackground = [UIImage imageNamed:@"cc.png"]; 22 23 //當輸入框沒有內容時,水印提示 提示內容爲password 24 text.placeholder = @"password"; 25 26 //設置輸入框內容的字體樣式和大小 27 text.font = [UIFont fontWithName:@"Arial" size:20.0f]; 28 29 //設置字體顏色 30 text.textColor = [UIColor redColor]; 31 32 //輸入框中是否有個叉號,在何時顯示,用於一次性刪除輸入框中的內容 33 text.clearButtonMode = UITextFieldViewModeAlways; 34 35 typedef enum { 36 UITextFieldViewModeNever, //重不出現 37 UITextFieldViewModeWhileEditing, //編輯時出現 38 UITextFieldViewModeUnlessEditing, //除了編輯外都出現 39 UITextFieldViewModeAlways //一直出現 40 } UITextFieldViewMode; 41 42 //輸入框中一開始就有的文字 43 text.text = @"一開始就在輸入框的文字"; 44 45 //每輸入一個字符就變成點 用語密碼輸入 46 text.secureTextEntry = YES; 47 48 //是否糾錯 49 text.autocorrectionType = UITextAutocorrectionTypeNo; 50 51 typedef enum { 52 UITextAutocorrectionTypeDefault, //默認 53 UITextAutocorrectionTypeNo, //不自動糾錯 54 UITextAutocorrectionTypeYes, //自動糾錯 55 } UITextAutocorrectionType; 56 57 //再次編輯就清空 58 text.clearsOnBeginEditing = YES; 59 60 //內容對齊方式 61 text.textAlignment = UITextAlignmentLeft; 62 63 //內容的垂直對齊方式 UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment 64 text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 65 66 //設置爲YES時文本會自動縮小以適應文本窗口大小.默認是保持原來大小,而讓長文本滾動 67 textFied.adjustsFontSizeToFitWidth = YES; 68 69 //設置自動縮小顯示的最小字體大小 70 text.minimumFontSize = 20; 71 72 //設置鍵盤的樣式 73 text.keyboardType = UIKeyboardTypeNumberPad; 74 75 typedef enum { 76 UIKeyboardTypeDefault, //默認鍵盤,支持全部字符 77 UIKeyboardTypeASCIICapable, //支持ASCII的默認鍵盤 78 UIKeyboardTypeNumbersAndPunctuation, //標準電話鍵盤,支持+*#字符 79 UIKeyboardTypeURL, //URL鍵盤,支持.com按鈕 只支持URL字符 80 UIKeyboardTypeNumberPad, //數字鍵盤 81 UIKeyboardTypePhonePad, //電話鍵盤 82 UIKeyboardTypeNamePhonePad, //電話鍵盤,也支持輸入人名 83 UIKeyboardTypeEmailAddress, //用於輸入電子 郵件地址的鍵盤 84 UIKeyboardTypeDecimalPad, //數字鍵盤 有數字和小數點 85 UIKeyboardTypeTwitter, //優化的鍵盤,方便輸入@、#字符 86 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 87 } UIKeyboardType; 88 89 //首字母是否大寫 90 text.autocapitalizationType = UITextAutocapitalizationTypeNone; 91 92 typedef enum { 93 UITextAutocapitalizationTypeNone, //不自動大寫 94 UITextAutocapitalizationTypeWords, //單詞首字母大寫 95 UITextAutocapitalizationTypeSentences, //句子的首字母大寫 96 UITextAutocapitalizationTypeAllCharacters, //全部字母都大寫 97 } UITextAutocapitalizationType; 98 99 //return鍵變成什麼鍵 100 text.returnKeyType =UIReturnKeyDone; 101 102 typedef enum { 103 UIReturnKeyDefault, //默認 灰色按鈕,標有Return 104 UIReturnKeyGo, //標有Go的藍色按鈕 105 UIReturnKeyGoogle, 106 //標有Google的藍色按鈕,用語搜索 107 UIReturnKeyJoin, 108 //標有Join的藍色按鈕 109 UIReturnKeyNext, 110 //標有Next的藍色按鈕 111 UIReturnKeyRoute, 112 //標有Route的藍色按鈕 113 UIReturnKeySearch, 114 //標有Search的藍色按鈕 115 UIReturnKeySend, 116 //標有Send的藍色按鈕 117 UIReturnKeyYahoo, 118 //標有Yahoo的藍色按鈕 119 UIReturnKeyYahoo, 120 //標有Yahoo的藍色按鈕 121 UIReturnKeyEmergencyCall, //緊急呼叫按鈕 122 } UIReturnKeyType; 123 124 //鍵盤外觀 125 textView.keyboardAppearance=UIKeyboardAppearanceDefault; 126 typedef enum { 127 UIKeyboardAppearanceDefault, //默認外觀,淺灰色 128 UIKeyboardAppearanceAlert, //深灰 石墨色 129 130 } UIReturnKeyType; 131 132 //設置代理 用於實現協議 133 text.delegate = self; 134 135 //把textfield加到視圖中 136 [self.window addSubview:text]; 137 138 //最右側加圖片是如下代碼 左側相似 139 UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]]; 140 text.rightView=image; 141 text.rightViewMode = UITextFieldViewModeAlways; 142 143 typedef enum { 144 UITextFieldViewModeNever, 145 UITextFieldViewModeWhileEditing, 146 UITextFieldViewModeUnlessEditing, 147 UITextFieldViewModeAlways 148 } UITextFieldViewMode; 149 150 //按return鍵鍵盤往下收 becomeFirstResponder 151 152 //類要採用UITextFieldDelegate協議 153 154 text.delegate = self; //聲明text的代理是我,我會去實現把鍵盤往下收的方法 這個方法在UITextFieldDelegate裏因此咱們要採用UITextFieldDelegate這個協議 155 156 - (BOOL)textFieldShouldReturn:(UITextField *)textField 157 { 158 [text resignFirstResponder]; //主要是[receiver resignFirstResponder]在哪調用就能把receiver對應的鍵盤往下收 159 return YES; 160 } 161 162 //重寫繪製行爲 163 //除了UITextField對象的風格選項,你還能夠定製化UITextField對象,爲他添加許多不一樣的重寫方法,來改變文本字段的顯示行爲。這些方法都會返回一個CGRect結構,制定了文本字段每一個部件的邊界範圍。如下方法均可以重寫。 164 165 – textRectForBounds: //重寫來重置文字區域 166 – drawTextInRect: 167 //改變繪文字屬性.重寫時調用super能夠按默認圖形屬性繪製,若本身徹底重寫繪製函數,就不用調用super了. 168 – placeholderRectForBounds: 169 //重寫來重置佔位符區域 170 – drawPlaceholderInRect: 171 //重寫改變繪製佔位符屬性.重寫時調用super能夠按默認圖形屬性繪製,若本身徹底重寫繪製函數,就不用調用super了. 172 – borderRectForBounds: 173 //重寫來重置邊緣區域 174 – editingRectForBounds: 175 //重寫來重置編輯區域 176 – clearButtonRectForBounds: 177 //重寫來重置clearButton位置,改變size可能致使button的圖片失真 178 – leftViewRectForBounds: 179 – rightViewRectForBounds: 180 181 //委託方法 182 183 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 184 185 //返回一個BOOL值,指定是否循序文本字段開始編輯 186 187 return YES; 188 } 189 190 - (void)textFieldDidBeginEditing:(UITextField *)textField{ 191 192 //開始編輯時觸發,文本字段將成爲first responder 193 } 194 195 - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ 196 197 //返回BOOL值,指定是否容許文本字段結束編輯,當編輯結束,文本字段會讓出first responder 198 199 //要想在用戶結束編輯時阻止文本字段消失,能夠返回NO 200 201 //這對一些文本字段必須始終保持活躍狀態的程序頗有用,好比即時消息 202 203 return NO; 204 } 205 206 - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ 207 208 //當用戶使用自動更正功能,把輸入的文字修改成推薦的文字時,就會調用這個方法。 209 //這對於想要加入撤銷選項的應用程序特別有用 210 //能夠跟蹤字段內所作的最後一次修改,也能夠對全部編輯作日誌記錄,用做審計用途。 211 //要防止文字被改變能夠返回NO 212 //這個方法的參數中有一個NSRange對象,指明瞭被改變文字的位置,建議修改的文本也在其中 213 214 return YES; 215 } 216 217 - (BOOL)textFieldShouldClear:(UITextField *)textField{ 218 219 //返回一個BOOL值指明是否容許根據用戶請求清除內容 220 //能夠設置在特定條件下才容許清除內容 221 222 return YES; 223 } 224 225 -(BOOL)textFieldShouldReturn:(UITextField *)textField{ 226 227 //返回一個BOOL值,指明是否容許在按下回車鍵時結束編輯 228 229 //若是容許要調用resignFirstResponder 方法,這回致使結束編輯,而鍵盤會被收起[textField resignFirstResponder]; 230 //查一下resign這個單詞的意思就明白這個方法了 231 232 return YES; 233 }