SwiftUI TextField: Deep Dive

TextField is a control that displays an editable text interface. TextField creates an input field that users can interact with. TextField provides the ability to read and write text for apps. UITextField is its counterpart in UIKit.

TextField uses bindable values to get and set text which enables us to work directly with data. Change in data is observed by binding variables and TextField can react to those changes.

In this section, we will take a closer look at SwiftUI's Text Field.

Basics

Start with a simple Text Field example.

Style

Styling TextField using TextFieldStyle

Alignment

Multiline Text Alignment in TextField

Auto Capitalization

Text Auto Capitalization in TextField

Font

Font modifier in TextField

Autocorrection

Autocorrection in TextField

Border

Border around TextField

Custom Border

Custom Border for TextField

Background Color

BackgroundColor for TextField

Foreground Color

ForegroundColor with TextField

Placeholder Color

Custom Color for Placeholder Text in TextField

Custom UI

Create Custom TextFields Views

Keyboard Type

KeyboardType Modifier for Context based Keyboard

Keyboard Open/Close

Working with keyboard state

Enable/Disable

Enable/Disable TextField with .disabled

OnSubmit

OnSubmit modifier for TextField

OnSubmitLabel

OnSubmitLabel for TextField

Focused

Focused and FocusState for TextField

OnChange

OnChange for TextField

OnEditingChange

OnEditingChange for TextField