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.
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:
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
.words
Use this for proper names or title-like text.
Examples:
- first and last names
- street names
- city names
.sentences
Use this for ordinary prose input.
Examples:
- comments
- chat composition
- note fields
.allCharacters
Use this when the UI should encourage uppercase entry.
Examples:
- some code or reference formats
- all-caps style fields
Objective-C Example
If you are working in older UIKit code:
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:
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
- '
autocapitalizationTypecontrols how iOS capitalizes text as the user types.' - Use
.nonefor email, usernames, and passwords. - Use
.wordsfor names and.sentencesfor 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
- UITextField border color
- UITextField text change event
- UITextField text change event
- UITextField value changed not firing when field is updated
- UITextView content inset
- UITextView Disable selection, allow links
- UITextView style is being reset after setting text property
- UITextView that expands to text using auto layout
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.