Generic UITextField in IOS

Generic UITextField

As iOS developers, most often we develop apps that have one or more forms prompting the user to enter his/her email address, date of birth, gender, phone number, etc. In such cases, we typically use UITextField and customize a few properties to launch different view pickers/keyboards.

Since this is very common in iOS apps, we have created a UITextField wrapper class called AITextField that allows us to use a single textfield to present different pickers/keyboards as required.

Why do we need a wrapper ?

The wrapper saves us precious time previously spent developing the most common UI behavior and allows us to focus on solving new problems instead.

Generic UITextField

How Do I Use AITextField?

Adding AITextField to your project is simple – just follow the steps below.

a) Using Through Interface Builder

  1. Drag and drop the UITextField in the screen.
  2. Update the class name to AITextField here.

Generic UITextField

3. Create an IBOutlet and use it in your class.

Generic UITextField

b) Using Programmatically.

Use the init with frame function and add the field to your view.

let myTextField = AITextField.init(frame: myFrame)
self.view.addSubview(myTextField)

For Date Picker

iOS Development

dateOfBirthTextField.textFieldType = .DatePickerTextField
dateOfBirthTextField.updateUIAsPerTextFieldType()

For Phone Pad

iOS Development

phoneNumberTextField.textFieldType = .PhoneNumberTextField
phoneNumberTextField.updateUIAsPerTextFieldType()

For Text Picker

iOS Development

genderTextField.pickerViewArray = [“Female”, “Male", “Others”]
genderTextField.textFieldType = .TextPickerTextField
genderTextField.updateUIAsPerTextFieldType()

You can also use this AITextField as a general UITextField.

 

Customizing AITextField

Adding Left and Right Views

myTextField.setLeftGap(width: 10, placeHolderImage: UIImage(named: “settings”)!)

or

myTextField.setRightGap(width: 10, placeHolderImage: UIImage(named: "copy")!)

Styling Input View

We can add a separator at the bottom of the text field using:

myTextField.createSeparator(borderColor: .darkGray, xpos: 10)

You can find the source code and AITextField class here.

Please drop any issues you found with this class into the comments so that I can update it to help new developers.

How to Make An App

10 Step Guide on How to Make an App

At AppIt Ventures, we know that the process of mobile app development can be daunting, especially without the right development partner. To give you a better idea of the typical process of how to make an app, we’ve put together this list of the 10 steps in developing and launching your mobile app, from conception to a successful and rewarding deployment.

DOWNLOAD A COPY of “10 Step Guide on How to Make an App

Categories

Download Your Free Mobile App Development eBook

App-Development

Subscribe to our newsletter!

Leave a Comment