How To Change UITextField Keyboard Type And Hide Keyboard Automatically When Press Return Key

All the swift interactive ui component ( UITextField, UITextView etc ) can prompt keyboard automatically when you move focus on it. And you can change the keyboard type both in interface builder Attributes Inspector panel and programmatically. This article will tell you how to set the keyboard type and how to hide the keyboard when you press return key in the keyboard.

1. How To Change UITextField Keyboard Type In Xcode Attribute Inspector.

  1. Click the UITextField ui component in Main.storyboard file to select it.
  2. Then click View —> Inspectors —> Show Attributes Inspector menu item at Xcode top menu bar.
    xcode view - inspectors menu item
  3. Scroll down to Text Input Traits section, there are three drop down list related to the keyboard. They are Keyboard Type, Keyboard Look and Return Key.
    xcode attributes inspector text input traits keyboard type
  4. The Keyboard Type drop down list contains below items : Default, Ascii Capable, Numbers And Punctuation, URL, Number Pad, Phone Pad, Name Phone Pad, Email Address, Decimal Pad, Twitter, Web Search, Ascii Capable Number Pad.
  5. The Keyboard Look drop down list contains below items : Default, Dark, Light.
  6. The Return Key drop down list contains below items : Default, Go, Google, Join, Next, Route, Search, Send, Yahoo, Done, Emergency Call, Continue. Please note if you choose Google as the return key, it will display word Search in the return key also.

2. How To Change UITextField Keyboard Type Programmatically.

Besides interface builder, you can also change the keyboard type, look and return key by code programmatically.

  1. The UIKit.UIKeyboardType class provide options for you to specify the keyboard type, it has an enum variable that contains all the keyboard type values.
  2. The UIKit.UIKeyboardAppearance class contains enum property to specify keyboard look.
  3. The¬†UIKit.UIReturnKeyType class contains enum property to specify the return key’s text in the keyboard.
  4. Below is an example that change the keyboard type, look and return key word. You can refer article How To Use UITextField And UITextView In Swift Programmatically Example to get the full source code.
    // Change the keyboard type to email address type.
    jobTitleTextField.keyboardType = UIKeyboardType.emailAddress
    
    // Change the keyboard appearance to dark.
    jobTitleTextField.keyboardAppearance = UIKeyboardAppearance.dark
    
    // Change the keyboard return key word to Go.
    jobTitleTextField.returnKeyType = UIReturnKeyType.go
  5. Below is the image picture when execute this example. When focus on the job title input text box, it will prompt the customized keyboard.
    change keyboard type look and return key word for uitextfield example
  6. When you focus on the job description text view, it will prompt the default keyboard, you can see the difference between them.
    display keyboard automatically in xcode ios simulator app when press text field
READ :   How To Customize Swift Slider In iOS App Example

3. How To Hide Keyboard When Press Return Key Programmatically In Xcode iOS Simulator.

  1. If the ViewController class implement UITextFieldDelegate delegate protocol, then it can implement textFieldShouldReturn function. This function will be called when you press return key in the UITextField keyboard. So we can add below code in textFieldShouldReturn function to make the UITextField’s keyboard resign the first responder when press the return key.
    // This function is called when you click return key in the text field.
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        print("textFieldShouldReturn")
        
        // Resign the first responder from textField to close the keyboard.
        textField.resignFirstResponder()
        
        return true
    }
  2. To hide the keyboard when user press return key in UITextView‘s keyboard. We can add below code in textView function. You should first make your ViewController class to implement UITextViewDelegate delegate protocol. Then you can implement textView function with below source code.
    // This function is called when you input text in the textView.
        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool{
            
            print("textView.")
            
            print("input text is : \(text)")
            
            // If user press return key in the keyboard.
            if("\n" == text){
                
                // Resign first responder from UITextView to close the keyboard.
                textView.resignFirstResponder()
                
                return false
            }
            
            return true
        }

Reference

  1. How To Use UITextField And UITextView In Swift Programmatically Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.