Programmatically change UITextField Keyboard type

前端 未结 12 1318
不知归路
不知归路 2020-11-28 19:28

Is it possible to programmatically change the keyboard type of a uitextfield so that something like this would be possible:

if(user is prompted for numeric i         


        
12条回答
  •  悲哀的现实
    2020-11-28 19:44

    There is a keyboardType property for a 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;
    

    Your code should read

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

提交回复
热议问题