iOS開發系列之三 - UITextField 用法小結

來源:互聯網
上載者:User

iOS開發系列之三 - UITextField 用法小結

// 初始化輸入框並設定位置和大小UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 30)];// 設定輸入框提示textField.placeholder = @"TextField Tip";// 輸入框中預先輸入的文字textField.text = @"預先輸入的文字";// 設定輸入框文本的字型textField.font = [UIFont fontWithName:@"Arial" size:20.0f];// 設定輸入框字型顏色textField.textColor = [UIColor redColor];// 設定輸入框的背景顏色textField.backgroundColor = [UIColor grayColor];// 設定輸入框邊框樣式textField.borderStyle = UITextBorderStyleRoundedRect;// 邊框樣式有以下幾種://    enum {//        UITextBorderStyleNone,        無邊框,預設//        UITextBorderStyleLine,        有線型邊框//        UITextBorderStyleBezel,       有線型邊框和陰影//        UITextBorderStyleRoundedRect  有圓角邊框//    } UITextBorderStyle;// 設定輸入框是否用於密碼textField.secureTextEntry = NO;// 設定是否有清除按鈕,在什麼時候顯示,用於一次性刪除輸入框中的所有內容textField.clearButtonMode = UITextFieldViewModeWhileEditing;// 清除按鈕樣式有以下幾種://    enum {//        UITextFieldViewModeNever,          從不出現//        UITextFieldViewModeWhileEditing,   編輯時出現//        UITextFieldViewModeUnlessEditing,  除了編輯外都出現//        UITextFieldViewModeAlways          一直出現//    } UITextFieldViewMode;// 設定自動錯誤修正方式textField.autocorrectionType = UITextAutocorrectionTypeNo;// 自動錯誤修正方式有以下幾種://    enum {//        UITextAutocorrectionTypeDefault,  預設//        UITextAutocorrectionTypeNo,       不自動錯誤修正//        UITextAutocorrectionTypeYes,      自動錯誤修正//    } UITextAutocorrectionType;// 設定自動大寫方式textField.autocapitalizationType = UITextAutocapitalizationTypeNone;// 自動大寫方式有以下幾種://    enum {//        UITextAutocapitalizationTypeNone,           不自動大寫//        UITextAutocapitalizationTypeWords,          單字首大寫//        UITextAutocapitalizationTypeSentences,      句子的首字母大寫//        UITextAutocapitalizationTypeAllCharacters,  所有字母都大寫//    } UITextAutocapitalizationType;// 設定再次編輯是否清空textField.clearsOnBeginEditing = YES;// 設定文本對齊textField.textAlignment = NSTextAlignmentLeft;// iOS7中文本對齊有以下幾種://    enum {//        NSTextAlignmentLeft      = 0,  靠左對齊,預設//        NSTextAlignmentCenter    = 1,  置中對齊//        NSTextAlignmentRight     = 2,  靠右對齊//        NSTextAlignmentJustified = 3,  在一個段落的最後一行自然對齊//        NSTextAlignmentNatural   = 4,  預設對齊//    } NSTextAlignment;// 設定字型大小是否自動適應輸入框寬度,預設是保持原來大小,長文本滾動textField.adjustsFontSizeToFitWidth = YES;// 設定自動縮小顯示的最小字型大小textField.minimumFontSize = 20;// 設定鍵盤的樣式textField.keyboardType = UIKeyboardTypeNumberPad;// 鍵盤樣式有以下幾種://    enum {//        UIKeyboardTypeDefault,                預設鍵盤,支援所有字元//        UIKeyboardTypeASCIICapable,           支援ASCII的預設鍵盤//        UIKeyboardTypeNumbersAndPunctuation,  標準電話鍵盤,支援+*#字元//        UIKeyboardTypeURL,                    只支援URL字元的URL鍵盤,支援.com按鈕//        UIKeyboardTypeNumberPad,              數字鍵台//        UIKeyboardTypePhonePad,               電話鍵盤//        UIKeyboardTypeNamePhonePad,           支援輸入人名的電話鍵盤//        UIKeyboardTypeEmailAddress,           電子郵件鍵盤//        UIKeyboardTypeDecimalPad,             有數字和小數點的數字鍵台//        UIKeyboardTypeTwitter,                最佳化的鍵盤,方便輸入@、#字元//        UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,//    } UIKeyboardType;// 設定return鍵樣式textField.returnKeyType = UIReturnKeyDone;// return鍵有以下幾種樣式://    enum {//        UIReturnKeyDefault,        預設,灰色按鈕,標有Return//        UIReturnKeyGo,             標有Go的藍色按鈕//        UIReturnKeyGoogle,         標有Google的藍色按鈕,用於搜尋//        UIReturnKeyJoin,           標有Join的藍色按鈕//        UIReturnKeyNext,           標有Next的藍色按鈕//        UIReturnKeyRoute,          標有Route的藍色按鈕//        UIReturnKeySearch,         標有Search的藍色按鈕//        UIReturnKeySend,           標有Send的藍色按鈕//        UIReturnKeyYahoo,          標有Yahoo的藍色按鈕//        UIReturnKeyYahoo,          標有Yahoo的藍色按鈕//        UIReturnKeyEmergencyCall,  緊急電話按鈕//    } UIReturnKeyType; // 設定鍵盤外觀textField.keyboardAppearance = UIKeyboardAppearanceDefault;// 鍵盤外觀有一下兩種://    enum {//        UIKeyboardAppearanceDefault, 預設面板,淺灰色//        UIKeyboardAppearanceAlert,   深灰,石墨色//    } UIReturnKeyType;// 設定代理,用於實現協議textField.delegate = self; // 最右側加圖片是以下代碼,左側類似UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];textField.rightView = image;textField.rightViewMode = UITextFieldViewModeAlways;// 把輸入框加到視圖中[self.view addSubview:textField];// 按return鍵收合鍵盤- (BOOL)textFieldShouldReturn:(UITextField *)textField{    [text resignFirstResponder];    return YES;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.