Generic UITextField in iOS

Designed by Meghanath Ganji, Image Credits: Giridhar Reddy Vennapusa

As an iOS developer most often we develop apps which generally has one or more forms asking user to enter Email, Date of Birth, Time, Gender, Phone number and etc. In such cases we use UITextField and customise few properties to launch different view pickers/keyboards.

As this seems to be very common in any iOS app, we have created a UITextField wrapper class called AITextField, so that we can use single textfield to present different pickers/keyboards as required.

Why we need a wrapper ?

To save some time in developing most common UI behaviour and spend more time solving new problems.

How Simple to use AITextField ?

Add AITextField to your project.

a. Using through interface builder

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

3. Create an IBOutlet and use in your class.

b. Using Programatically.

Use the Init with frame function add the field to your view.

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

For Date Picker

dateOfBirthTextField.textFieldType = .DatePickerTextFielddateOfBirthTextField.updateUIAsPerTextFieldType()

For Phone Pad

phoneNumberTextField.textFieldType = .PhoneNumberTextFieldphoneNumberTextField.updateUIAsPerTextFieldType()

For TextPicker

stateTextField.pickerViewArray = [“ALASKA”, “ARIZONA", “ARKANSAS”, "CALIFORNIA",....]stateTextField.textFieldType = .TextPickerTextField
stateTextField.updateUIAsPerTextFieldType()

You can also use this AITextField as a general UITextField.

Customising AITextField

Adding left and right views

myTextField.setLeftGap(width: 10, placeHolderImage: UIImage(named: “settings”)!)ormyTextField.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)

Check below github repo for AITextField class and a demo app.

If you liked this post and found it helpful, give me some claps! Feel free to drop your comments on this topic. Follow AppIt Ventures for other similar articles. Thanks for reading 😄

--

--