UITextField Klavye türünü programlı olarak değiştirme


175

Bir uitextfield'ın klavye türünü programlı olarak değiştirmek mümkün müdür, böylece böyle bir şey mümkün olur:

if(user is prompted for numeric input only)
    [textField setKeyboardType: @"Number Pad"];

if(user is prompted for alphanumeric input)
    [textField setKeyboardType: @"Default"];

3
terimi doozydaha yaygın olarak anlaşılabilir bir şeye değiştirmenizi öneririm .. unutmayın SO uluslararası bir site ve Kuzey Amerika değil
— abbood

Yanıtlar:


371

Şunun için bir keyboardTypemülk vardır UITextField:

typedef enum {
    UIKeyboardTypeDefault,                // Default type for the current input method.
    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
    UIKeyboardTypeDecimalPad,             // A number pad including a decimal point
    UIKeyboardTypeTwitter,                // Optimized for entering Twitter messages (shows # and @)
    UIKeyboardTypeWebSearch,              // Optimized for URL and search term entry (shows space and .)

    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

} UIKeyboardType;

Kodunuz okunmalı

if(user is prompted for numeric input only)
    [textField setKeyboardType:UIKeyboardTypeNumberPad];

if(user is prompted for alphanumeric input)
    [textField setKeyboardType:UIKeyboardTypeDefault];

7
Bunun akıllı / bozuk bir kullanıcının başka karakterler girmesini engellemediğini unutmayın. Örneğin: Emoji klavyesi sayı alanınıza dokunmadan önce etkinse , içine gülen yüzler yazabilirler. Bu konuda yapabileceğiniz hiçbir şey yoktur ve kesinlikle Apple'ın hatasıdır, ancak bir sayı alanında sayı olmayan numaralar alırsanız kodunuzun çökmediğinden emin olmalısınız .
— Steven Fisher

Bugün öğrendim UITextInputTraits, bu UITextFieldbenimseyen protokolün bir özelliğidir .
— rounak

1
Web görünümünde yüklenen HTML giriş alanları için klavye türünü UIKeyboardTypeNumbersAndPunctuation olarak programlı olarak değiştirmek mümkün müdür?
— Srini

Uitextfield'ı ilgili klavye türüne sahip bir projede programlı olarak oluşturdum. bu birkaç gün önce uyandı. ama şimdi bu işe yaramadı. Gerçek bir sebep elde
— edemiyorum

78

Şu anda odaklanmış bir alanın klavye türünü hemen güncellemesini istiyorsanız , fazladan bir adım olduğunu belirtmek gerekir :

// textField is set to a UIKeyboardType other than UIKeyboardTypeEmailAddress

[textField setKeyboardType:UIKeyboardTypeEmailAddress];
[textField reloadInputViews];

Çağrı reloadInputViewsyapılmazsa, seçilen alan ( ilk cevaplayıcı ) kaybedip odaklanana kadar klavye değişmez .

UIKeyboardTypeDeğerlerin tam bir listesini burada bulabilirsiniz veya:

typedef enum : NSInteger {
    UIKeyboardTypeDefault,
    UIKeyboardTypeASCIICapable,
    UIKeyboardTypeNumbersAndPunctuation,
    UIKeyboardTypeURL,
    UIKeyboardTypeNumberPad,
    UIKeyboardTypePhonePad,
    UIKeyboardTypeNamePhonePad,
    UIKeyboardTypeEmailAddress,
    UIKeyboardTypeDecimalPad,
    UIKeyboardTypeTwitter,
    UIKeyboardTypeWebSearch,
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable
} UIKeyboardType;

Bu bilmek için yararlı bilgiler-sadece şu anda odaklanmış alan giriş değişikliği hakkında bilgi çıkarmak için bir soru cevap tarzı bir soru yapmayı öneririm (bu yanıtı bulduğumda aradığım şey)
— Stonz2

1
Ayrıca şu anda odaklanmamış olan metin alanında reloadInputViews çağrılmasının klavye türünü hemen değiştirmeyeceğini belirtmek gerekir. İlk önce [textfield becameFirstResponder] öğesini ve ardından [textField reloadInputViews]
— öğesini

1
O oldu [textField reloadInputViews];ben eksik olduğunu. Teşekkürler!
— İslam S.

1
Calling reloadInputViewsayrıca ısmarlama UITextInputuygulamalar için de çalışır .
— Paul Gardiner

23

Evet şunları yapabilirsiniz:

[textField setKeyboardType:UIKeyboardTypeNumberPad];

9
    textFieldView.keyboardType = UIKeyboardType.PhonePad

Bu hızlı. Ayrıca bunun düzgün çalışması için,textFieldView.delegate = self


7

metin alanının alfa sayısal kabul etmesini sağlamak için yalnızca bu özelliği ayarlayın

textField.keyboardType = UIKeyboardTypeNamePhonePad;

6
_textField .keyboardType = UIKeyboardTypeAlphabet;
_textField .keyboardType = UIKeyboardTypeASCIICapable;
_textField .keyboardType = UIKeyboardTypeDecimalPad;
_textField .keyboardType = UIKeyboardTypeDefault;
_textField .keyboardType = UIKeyboardTypeEmailAddress;
_textField .keyboardType = UIKeyboardTypeNamePhonePad;
_textField .keyboardType = UIKeyboardTypeNumberPad;
_textField .keyboardType = UIKeyboardTypeNumbersAndPunctuation;
_textField .keyboardType = UIKeyboardTypePhonePad;
_textField .keyboardType = UIKeyboardTypeTwitter;
_textField .keyboardType = UIKeyboardTypeURL;
_textField .keyboardType = UIKeyboardTypeWebSearch;

5

Hızlı 4

Bir koşul karşılandığında klavye türünüzü değiştirmeye çalışıyorsanız, bunu izleyin. Örneğin: Metin alanının sayısı 4 veya 5 olduğunda klavye türünü Varsayılan yerine Sayısal Tuş Takımı olarak değiştirmek istiyorsak , bunu yapın:

textField.addTarget(self, action: #selector(handleTextChange), for: .editingChanged)

@objc func handleTextChange(_ textChange: UITextField) {
 if textField.text?.count == 4 || textField.text?.count == 5 {
   textField.keyboardType = .numberPad
   textField.reloadInputViews() // need to reload the input view for this to work
 } else {
   textField.keyboardType = .default
   textField.reloadInputViews()
 }

2

Bunun için bir özellik var keyboardType. Ne yapmak isteyeceksiniz Eğer dizeleri sahip olduğu yerini ise @"Number Padve @"Defaultile UIKeyboardTypeNumberPadve UIKeyboardTypeDefault.

Yeni kodunuz şöyle görünmelidir:

if(user is prompted for numeric input only)
    [textField setKeyboardType:UIKeyboardTypeNumberPad];

else if(user is prompted for alphanumeric input)
    [textField setKeyboardType:UIKeyboardTypeDefault];

İyi şanslar!


1

UIDatePickergirdi olarak kullanmak isteyenler için :

UIDatePicker *timePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 0, 0)];
[timePicker addTarget:self action:@selector(pickerChanged:)
     forControlEvents:UIControlEventValueChanged];
[_textField setInputView:timePicker];

// pickerChanged:
- (void)pickerChanged:(id)sender {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"d/M/Y"];
    _textField.text = [formatter stringFromDate:[sender date]];
}

1

Bu UIKeyboardTypesSwift 3:

public enum UIKeyboardType : Int {

    case `default` // Default type for the current input method.
    case asciiCapable // Displays a keyboard which can enter ASCII characters
    case numbersAndPunctuation // Numbers and assorted punctuation.
    case URL // A type optimized for URL entry (shows . / .com prominently).
    case numberPad // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.
    case phonePad // A phone pad (1-9, *, 0, #, with letters under the numbers).
    case namePhonePad // A type optimized for entering a person's name or phone number.
    case emailAddress // A type optimized for multiple email address entry (shows space @ . prominently).

    @available(iOS 4.1, *)
    case decimalPad // A number pad with a decimal point.

    @available(iOS 5.0, *)
    case twitter // A type optimized for twitter text entry (easy access to @ #)

    @available(iOS 7.0, *)
    case webSearch // A default keyboard type with URL-oriented addition (shows space . prominently).

    @available(iOS 10.0, *)
    case asciiCapableNumberPad // A number pad (0-9) that will always be ASCII digits.


    public static var alphabet: UIKeyboardType { get } // Deprecated
}

Bu, listeden bir klavye türü kullanmak için bir örnektir:

textField.keyboardType = .numberPad

0

UITextField Klavye türü swift 3.0'ı programlı olarak değiştirme

lazy var textFieldTF: UITextField = {

    let textField = UITextField()
    textField.placeholder = "Name"
    textField.frame = CGRect(x:38, y: 100, width: 244, height: 30)
    textField.textAlignment = .center
    textField.borderStyle = UITextBorderStyle.roundedRect
    textField.keyboardType = UIKeyboardType.default //keyboard type
    textField.delegate = self
    return textField 
}() 
override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(textFieldTF)
}

0

İşte Swift 4.2'deki klavye türü

// UIKeyboardType
//
// Requests that a particular keyboard type be displayed when a text widget
// becomes first responder. 
// Note: Some keyboard/input methods types may not support every variant. 
// In such cases, the input method will make a best effort to find a close 
// match to the requested type (e.g. displaying UIKeyboardTypeNumbersAndPunctuation 
// type if UIKeyboardTypeNumberPad is not supported).
//
public enum UIKeyboardType : Int {


    case `default` // Default type for the current input method.

    case asciiCapable // Displays a keyboard which can enter ASCII characters

    case numbersAndPunctuation // Numbers and assorted punctuation.

    case URL // A type optimized for URL entry (shows . / .com prominently).

    case numberPad // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.

    case phonePad // A phone pad (1-9, *, 0, #, with letters under the numbers).

    case namePhonePad // A type optimized for entering a person's name or phone number.

    case emailAddress // A type optimized for multiple email address entry (shows space @ . prominently).

    @available(iOS 4.1, *)
    case decimalPad // A number pad with a decimal point.

    @available(iOS 5.0, *)
    case twitter // A type optimized for twitter text entry (easy access to @ #)

    @available(iOS 7.0, *)
    case webSearch // A default keyboard type with URL-oriented addition (shows space . prominently).

    @available(iOS 10.0, *)
    case asciiCapableNumberPad // A number pad (0-9) that will always be ASCII digits.


    public static var alphabet: UIKeyboardType { get } // Deprecated
}
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.