標籤:
簡單說明:此控制項一般用於使用者的賬戶,密碼,驗證碼等需要輸入簡短內容的輸入視窗
(由於剛剛學完控制項部分,所以最近幾篇都是關於各控制項的介紹..)
UITextView與UITextField間的主要區別在於:UITextField可以設定InputText的相關屬性,例如可以設定SecureInput等相關屬性,而UITextView只能實現基本的文本輸入的功能,幾乎無法設定內容屬性。
UITextField *TF = [[UITextField alloc] initWithFrame:CGRectMke(90,100,140,25)]; //UITextField 初始化:
[self.window addSubview:TF]; // 添加到視圖
TF.textColor = [UIColor redColor]; // 輸入的字型顏色
TF.delegate = self; //設定代理,要在.h檔案上添加代理 @interface AppDelegate:UIResponder <UIApplicationDelegate,UITextFieldDelegate>
(代理的用處: 1.監聽那些不能通過addTarget監聽的事件!
2.主要用來負責在兩個對象之間,發生某些事件時,來傳遞訊息或者資料)
TF.placeholder = @“請輸入密碼: ”; // 當沒有輸入內容時,有浮水印字型提示
TF.textColor = [UIColor reColor ]; //輸入字型的顏色
TF.secureTextEntry = YES; //輸入內容是否可見
TF.tag = 101; // 給個tag值
TF.adjustsFontSizeToFitWidth = YES; //當輸入字元長度大於架構長度時,自動縮小字元以適應架構長度
TF.background = [UIImage imageNamed:@“***.jpg”]; //背景,當使用圖片為背景時,需要把TF.borderStyle設定為None
TF.backgroundColor = [UIColor yellowColor]; //當背景為顏色時,TF.borderStyle不能設為None
TF.borderStyle = UITextBorderStyleNone; //沒有樣式,直角邊框
UITextBorderStyleLine; //邊框為黑邊,直角邊框
UITextBorderStyleBezel; //有點陰影,直角邊框
UITextBorderStyleRoundedRect; // 圓角框
[TF becomeFirstResponder]; // 當渲染視圖後,TF自動成為第一響應者,彈出鍵盤,也可以在代理方法裡進行
// 輸入框裡的叉號,出現形式
TF.clearButtonMode = UITextFieldViewModeWhileEding; // 編輯時出現
UITextFieldViewModeNever; //從不出現
UITextFieldViewModeUnlessEditing; // 除了編輯時,其他時間都出現
UITextFieldViewModeAlways; // 一直都出現
TF.autocorrectionType = UITextAutocorrectionTypeNo; //不自動錯誤修正
UITextAutocorrectionTypeDefault; //預設
UITextAutocorrectionTypeYes; //自動錯誤修正
TF.clearsOnBeginEditing = YES; //重新點擊編輯時,是否清空上次輸入的內容,後面也有代理方法
//設定彈出的鍵盤的類型
TF.keyboardType = UIKeyboardTypeDefault; //預設鍵盤,支援所有字元
UIKeyboardTypeASCIICapable; //支援ASCII的預設鍵盤
UIKeyboardTypeNumbersAndPunctuation ; //標準電話鍵盤,支援+*#字元
UIKeyboardTypeURL; //URL鍵盤,支援.com按鈕 只支援URL字元
UIKeyboardTypeNumberPad; //數字鍵台
UIKeyboardTypePhonePad; //電話鍵盤
UIKeyboardTypeNamePhnoePad; //電話鍵盤,也支援輸入人名
UIKeyboardTypeEmailAddress; //電子郵件鍵盤
UIKeyboardTypeDecimalPad; //數字鍵台,有小數點
UIKeyboardTypeTwitter; //最佳化鍵盤
//首字母是否大寫
TF.autocapitalizationType = UITextAutocapitalizationTypeNone; //不自動大寫
UITextAutocapitalizationTypeWords,; //單字首大寫
UITextAutocapitalizationTypeSentences; //句子的首字母大寫
UITextAutocapitalizationTypeAllCharacters; // 所有字母都大寫
//return鍵變成什麼鍵
TF.returnKeyType =UIReturnKeyDone; //標有Done的藍色按鈕
UIReturnKeyDefault; //預設 灰色按鈕,標有Return
UIReturnKeyGo; //標有Go的藍色按鈕
UIReturnKeyGoogle; //標有Google的藍色按鈕,用語搜尋
UIReturnKeyJoin; //標有Join的藍色按鈕
UIReturnKeyNext; //標有Next的藍色按鈕
UIReturnKeyRoute; //標有Route的藍色按鈕
UIReturnKeySearch; //標有Search的藍色按鈕
UIReturnKeySend; //標有Send的藍色按鈕
UIReturnKeyYahoo; //標有Yahoo的藍色按鈕
UIReturnKeyYahoo; //標有Yahoo的藍色按鈕
UIReturnKeyEmergencyCall; //緊急電話按鈕
//UITextField的代理方法 (需要先設定代理) 注意是否要設定傳回值
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; //當使用者點擊到,並準備開始編輯內容時,使用方法,一邊是快顯視窗
- (void)textFieldDidBeginEditing:(UITextField *)textField; //當使用者開始編輯時,使用此代理方法
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; //當使用者準備結束編輯時,使用此代理方法
- (void)textFieldDidEndEditing:(UITextField *)textField; //當使用者結束編輯時,使用此代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location >= 6) {
return NO;
}else
return YES;
} // 當使用者使用自動校正功能,把輸入的文字修改為推薦的文字時,又或者限制輸入長度時, 就會調用這個方法。
- (BOOL)textFieldShouldClear:(UITextField *)textField; //當使用清除輸入內容功能時,使用此代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField; //當點擊鍵盤裡的Return收合鍵盤時,使用此代理方法
(可能學得比較淺,大家要是有補充可以留言,有問題的大家一起探討下,可以私下聯絡:QQ790444804 :hgwchihuo
-------我是快樂的小尾巴`(*∩_∩*)′)
IOS:UI設計之UITextField相關基礎