How to Use TextField in Swift to Enable Entry and Editing of Text

While there are many user interfaces on IPhone app development to create fun things using sliders, buttons, gestures, etc. But, when you write any iPhone app, no matter how innovative it is, implementing the good old-fashioned Swift text field will require at some point. The swift text field allows developer to display text and enable the entry and editing the text in iPhone apps.

Therefore, in our today’s Swift app development tutorial, we’re going to build a simple project to share the implementation process of Swift text field to collect text input in iPhone app.

Let’s Get Started

hand icon Open XCode and create a new project under file menu.

sampletextfield1

hand icon In the next tab, select Single View Application and click on Next.

sampletextfield2

hand icon Write the project name and other details.

sampletextfield3

hand icon Once you create a new project, go to Main.Storyboard and select ViewController.

sampletextfield4

hand icon Now drag and drop UITextField from “Show the object library” section.

sampletextfield5

sampletextfield6

hand icon Now select UITextField and see the attribute inspector which have properties for textfield. We can manage Text type, placeholder, color, font, border style, Keyboard type, return key, Security text for password field etc.

sampletextfield7

hand icon Now, set outlet UITextField in ViewController.

@IBOutlet weak var txtName: UITextField! 
Copy to Clipboard

set outlet UITextField in ViewController

hand icon Set delegate of UITextField.

class ViewController: UIViewController, UITextFieldDelegate
Copy to Clipboard

Declare Textfield delegate.

//MARK: Textfield Delegate
// When user press the return key in keyboard
func textFieldShouldReturn(textField: UITextField) -> Bool {
    print("textFieldShouldReturn")
    textField.resignFirstResponder()
    return true
}

// It is called before text field become active
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    textField.backgroundColor = UIColor.lightGray
    return true
}

// It is called when text field activated
func textFieldDidBeginEditing(textField: UITextField) {
    print("textFieldDidBeginEditing")
}

// It is called when text field going to inactive
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    textField.backgroundColor = UIColor.white
    return true
}

// It is called when text field is inactive
func textFieldDidEndEditing(textField: UITextField) {
    print("textFieldDidEndEditing")
}

// It is called each time user type a character by keyboard
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    print(string)
    return true
}
Copy to Clipboard

And done!

With this basic understanding of Swift text field, you’ll be able to easily add different text fields for your iPhone app development project. And, in case you’ve any questions regarding this iOS app tutorial, shoot them right away! Our team of swift experts will be happy to help.

Grab a free copy of Swift UITextfield tutorial from Github.

Bhaval Patel

Written by

Bhaval Patel is a Director (Operations) at Space-O Technologies. He has 20+ years of experience helping startups and enterprises with custom software solutions to drive maximum results. Under his leadership, Space-O has won the 8th GESIA annual award for being the best mobile app development company. So far, he has validated more than 300 app ideas and successfully delivered 100 custom solutions using the technologies, such as Swift, Kotlin, React Native, Flutter, PHP, RoR, IoT, AI, NFC, AR/VR, Blockchain, NFT, and more.

  • Get a Free Consultation No, continue to