UITextField
auto-capitalization
iPhone app development
iOS programming
Swift language

UITextField auto-capitalization type - iPhone App

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

UITextField has an autocapitalizationType property that tells iOS how to capitalize user input automatically as the keyboard is used. It is a small property, but choosing the right mode can make forms feel polished and can prevent frustrating input mistakes in fields such as names, sentences, email addresses, and passwords.

Available Capitalization Modes

The main options are:

  • '.none'
  • '.words'
  • '.sentences'
  • '.allCharacters'

Example in Swift:

swift
let textField = UITextField()
textField.autocapitalizationType = .words

Each mode is appropriate for a different kind of input.

When to Use Each Mode

.none

Use this for values where capitalization would be harmful or misleading.

Examples:

  • email addresses
  • usernames
  • passwords
  • promo codes that should remain exactly typed
swift
emailField.autocapitalizationType = .none
emailField.autocorrectionType = .no

.words

Use this for proper names or title-like text.

Examples:

  • first and last names
  • street names
  • city names
swift
nameField.autocapitalizationType = .words

.sentences

Use this for ordinary prose input.

Examples:

  • comments
  • chat composition
  • note fields
swift
messageField.autocapitalizationType = .sentences

.allCharacters

Use this when the UI should encourage uppercase entry.

Examples:

  • some code or reference formats
  • all-caps style fields
swift
codeField.autocapitalizationType = .allCharacters

Objective-C Example

If you are working in older UIKit code:

objective-c
self.nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;
self.emailField.autocapitalizationType = UITextAutocapitalizationTypeNone;

The underlying idea is the same as in Swift.

Capitalization and Other Keyboard Behaviors

autocapitalizationType should usually be chosen together with related settings such as:

  • 'keyboardType'
  • 'autocorrectionType'
  • 'textContentType'
  • 'isSecureTextEntry'

For example, an email field often wants all of these coordinated:

swift
1emailField.keyboardType = .emailAddress
2emailField.autocapitalizationType = .none
3emailField.autocorrectionType = .no
4emailField.textContentType = .emailAddress

The user experience is much better when these settings are consistent.

Be Careful With User Expectations

A field that auto-capitalizes incorrectly can be surprisingly annoying. If a username field capitalizes the first letter automatically, users may submit the wrong value without noticing. So the right capitalization mode is not just cosmetic. It affects correctness.

Interface Builder and Programmatic Setup

You can configure capitalization either in code or in Interface Builder. The important thing is consistency. If a nib or storyboard sets one value and runtime code sets another, debugging input behavior becomes confusing quickly.

For reusable forms, many teams prefer setting keyboard behavior in one place such as a field-configuration helper so capitalization, correction, and content type stay aligned across the app.

Test With Real Keyboard Scenarios

Capitalization behavior can feel correct in a quick simulator test and still be awkward in real forms. Test with actual user flows such as entering an email after auto-fill, editing the middle of an existing string, and switching between hardware and software keyboards.

That kind of testing often reveals whether the chosen capitalization mode is helping or just getting in the user's way.

Common Pitfalls

A common mistake is leaving the default capitalization behavior on fields like email and username inputs.

Another mistake is enabling capitalization without disabling autocorrection where autocorrection would also be inappropriate.

Developers also sometimes use .allCharacters to force formatting when the real requirement is validation or transformation after input. Keyboard hints help, but they do not replace validation logic.

Summary

  • 'autocapitalizationType controls how iOS capitalizes text as the user types.'
  • Use .none for email, usernames, and passwords.
  • Use .words for names and .sentences for prose-like input.
  • Coordinate capitalization with keyboard type and autocorrection settings.
  • Choose the mode based on correctness of the input, not only appearance.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.