您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發系列之三:UITextField 屬性用法完整整理

編輯: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];

二、邊框樣式 borderStyle

textField.borderStyle = UITextBorderStyleRoundedRect;
  • UITextBorderStyleNone:無邊框(預設)。
  • UITextBorderStyleLine:線型邊框。
  • UITextBorderStyleBezel:線型邊框加內陰影。
  • UITextBorderStyleRoundedRect:圓角邊框。

注意:使用 Bezel 或 RoundedRect 時,borderColor 與 borderWidth 不會生效,因為樣式由系統接管。

三、顯示行為

textField.secureTextEntry = NO;   // 設為 YES 即以圓點顯示(密碼欄)
textField.clearsOnBeginEditing = YES;  // 再次進入編輯時自動清空
textField.clearButtonMode = UITextFieldViewModeWhileEditing;  // 清除鈕顯示時機

清除鈕的四種時機:

  • UITextFieldViewModeNever:永不顯示。
  • UITextFieldViewModeWhileEditing:編輯時顯示。
  • UITextFieldViewModeUnlessEditing:非編輯時顯示。
  • UITextFieldViewModeAlways:永遠顯示。

四、輸入輔助

textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;

自動更正有 Default / No / Yes 三種;自動大寫有 None(不轉換)、Words(每字首字母)、Sentences(句首)、AllCharacters(全大寫)四種。帳號、密碼、序號欄位通常應該都關掉。

五、文字對齊與自動縮放

textField.textAlignment = NSTextAlignmentLeft;
textField.adjustsFontSizeToFitWidth = YES;   // 過長時自動縮小字級
textField.minimumFontSize = 20;              // 縮小的下限

對齊方式:Left、Center、Right、Justified(段落最後一行自然對齊)、Natural(依語系方向決定,多語言 App 建議用這個)。

六、鍵盤相關

textField.keyboardType = UIKeyboardTypeNumberPad;
textField.returnKeyType = UIReturnKeyDone;
textField.keyboardAppearance = UIKeyboardAppearanceDefault;

常見 keyboardType:

  • Default:支援所有字元。
  • ASCIICapable:支援 ASCII 的預設鍵盤。
  • NumbersAndPunctuation:電話鍵盤,含 + * #。
  • URL:帶 .com 鍵。
  • NumberPad:純數字(沒有 Return 鍵)。
  • PhonePad:電話號碼。
  • NamePhonePad:可輸入人名的電話鍵盤。
  • EmailAddress:電子郵件。
  • DecimalPad:數字加小數點(沒有 Return 鍵)。
  • Twitter:方便輸入 @ 與 #。

returnKeyType 可選 Default、Go、Google、Join、Next、Route、Search、Send、Yahoo、EmergencyCall。語意要與實際行為一致。

keyboardAppearance 有 Default(淺灰)與 Alert(深灰石墨色)兩種。

七、左右側裝飾

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
textField.rightView = image;
textField.rightViewMode = UITextFieldViewModeAlways;
// leftView 與 leftViewMode 用法相同

八、代理:收起鍵盤

textField.delegate = self;
[self.view addSubview:textField];

#pragma mark - UITextFieldDelegate

// 按下 Return 時收起鍵盤
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

其他實用上經常實作的 delegate 方法:

  • textFieldShouldBeginEditing::回傳 NO 可禁止進入編輯。
  • textFieldDidBeginEditing::進入編輯,適合啟動日期選擇器。
  • textFieldShouldClear::按下清除鈕前,回傳 NO 可攔截。
  • textField:shouldChangeCharactersInRange:replacementText::可在此限制長度或攔截特定字元。

補充:UITextField 的取代者

UITextField 只支援單行輸入。若需要多行、字數統計或 placeholder 樣式客製,請改用 UITextView;若需要更完整的表單驗證與錯誤提示,iOS 10 之後提供的 UITextInputTraits 搭配 textContentType 能讓系統更準確地輔助自動填寫。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved