Programmatically Select all text in UITextField
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To select all text in a UITextField, the field must be active and you must assign a full UITextRange to selectedTextRange. The usual mistake is trying to set the selection before the text field becomes first responder, which makes the call appear to do nothing.
Basic Selection Pattern
The core idea is:
- make the text field first responder
- build a range from
beginningOfDocumenttoendOfDocument - assign that range to
selectedTextRange
Here is the direct Swift version:
If the field is on screen and able to become first responder, that selects the full text.
A View Controller Example
This is a realistic example with a button that selects the text programmatically:
This is the standard UIKit pattern and works well in normal user-driven flows.
Selecting When Editing Begins
If you want the whole text selected automatically when the user enters the field, the right place is often the delegate callback.
The DispatchQueue.main.async is sometimes useful because it lets the responder transition complete before selection is applied.
Why Selection Sometimes Fails
Programmatic selection depends on timing and responder state. If the field is not active yet, UIKit may ignore the selection request or immediately replace it during editing setup.
That is why code that looks correct can fail when called:
- too early in the view lifecycle
- before the field is on screen
- before the field becomes first responder
When in doubt, perform selection after becomeFirstResponder() and, if needed, on the next main-thread turn.
Select All Versus Cursor Placement
There is also a behavioral difference between putting the cursor at the end of the text and selecting the entire range. If you only call becomeFirstResponder(), UIKit will usually place the insertion point somewhere appropriate, but it will not automatically highlight everything. Full replacement behavior requires an actual range assignment.
That distinction is important in forms where you want the next keystroke to replace the existing value instead of appending to it.
Common Pitfalls
- Setting
selectedTextRangebefore the field becomes first responder. - Trying to select text before the field is actually visible and interactive.
- Forgetting to check that the text range was created successfully.
- Applying the selection too early in delegate flow and then wondering why UIKit overwrote it.
- Assuming programmatic selection will work the same way for every custom text input control. This pattern is for
UITextField.
Summary
- To select all text in a
UITextField, make it first responder and setselectedTextRangeto the full document range. - '
beginningOfDocumentandendOfDocumentdefine the full selectable span.' - '
textFieldDidBeginEditingis a good place to auto-select when editing starts.' - If timing is unreliable, defer the selection with
DispatchQueue.main.async. - Most failures happen because the field is not yet active when the selection is requested.
Related reading
- Programmatically selecting text in an input field on iOS devices mobile Safari
- Programmatically set image to UIImageView with Xcode 6.1/Swift
- Programmatically set left drawable in a TextView
- Programmatically set the initial view controller using Storyboards
- Programmatically set the initial view controller using Storyboards
- Programmatically switching between tabs within Swift
- Programming with SurfaceView and thread strategy for game development
- ProgressDialog is deprecated.What is the alternate one to use?
.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.