GlassTextField
Text field with label, validation message, focus handling and keyboard configuration.
- Content
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-text-field
Content on an opaque, readable surface. Glass is kept for controls.
CaptureInstallation
spglass add glass-text-fieldCopies the sources and their dependencies into your project. Set up the CLI.
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")
import SPGlassKit| Identifier | glass-text-field |
|---|---|
| Module | SPGlassForms |
| Kind | content-component — a system TextField on an opaque themed surface with glassFieldOutline |
| Platforms | iOS 26, iPadOS 26, macOS 26 |
| Example | Examples/Gallery/Forms/GlassTextFieldExample.swift |
Minimal usage
@State private var name = ""
GlassTextField("Name", text: $name)Variants
| Variant | Use |
|---|---|
prompt: | Placeholder shown while the field is empty. |
helper: | Explanation under the field, replaced by the error when there is one. |
validation: | Rules checked in order: .required(), .minLength(_:), .maxLength(_:), .custom(message:_:). |
validateOn: | .afterEditing (default: once the field was left or submitted), .always, .never. |
errorMessage: | An error decided by the host (server answer). It replaces the validation message. |
systemImage: | An SF Symbol at the leading edge of the field ("envelope", "at"); it takes the primary colour while the field is focused. |
successMessage: | A confirmation decided by the host ("This name is available."): success outline, check mark in the field and the sentence under it. |
input: | .standard, .email, .username, .url, .number, .name, or a custom GlassTextInputConfiguration. |
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
label | LocalizedStringKey | — | Label above the field; also its accessibility label. |
text | Binding<String> | — | The value. |
prompt | LocalizedStringKey? | nil | Placeholder. |
helper | LocalizedStringKey? | nil | Helper text. |
validation | GlassFieldValidation | .none | Rules of the field. |
validateOn | GlassValidationTrigger | .afterEditing | When the message starts to show. |
systemImage | String? | nil | Leading SF Symbol, decorative (1.1). |
errorMessage | String? | nil | External error. |
successMessage | String? | nil | Confirmation, already localised; shown when there is no error (1.1). |
input | GlassTextInputConfiguration | .standard | Autocorrection, capitalisation, keyboard, content type. |
focus | FocusState<Bool>.Binding? | nil | Focus owned by the host. |
GlassTextInputConfiguration holds autocorrectionDisabled, capitalization, keyboard and contentKind. Capitalisation and keyboard apply on iOS and iPadOS only.
States
Idle (opaque surface with a hairline), focused (outline in the theme's focusRing colour plus a crisp outer ring), error (outline in destructive, a mark inside the field, icon and message under it), success (successMessage: outline in success, check mark, confirmation), disabled (.disabled(_:), dimmed). A required field shows an asterisk next to its label and announces "Required".
Advanced example
@State private var email = ""
@State private var serverError: String?
@FocusState private var emailFocused: Bool
GlassTextField(
"Email", text: $email, prompt: "name@example.com",
validation: GlassFieldValidation(
.required(),
.custom(message: String(localized: "Enter a valid address")) { $0.contains("@") }
),
errorMessage: serverError,
input: .email,
focus: $emailFocused
)
.submitLabel(.next)
.onSubmit { register() }The rules are pure and can be checked before submitting: validation.isValid(email).
Accessibility
- The visible label is the accessibility label of the field; "Required" is appended for required fields.
- An error is an icon plus a sentence, read as "Error: …"; a confirmation is a check mark plus a sentence. Neither is colour alone.
- The leading symbol is decorative and hidden from VoiceOver.
- Label, value and messages follow Dynamic Type and wrap.
- Default messages are localised (English, French); lengths count user-perceived characters.
Limits
- Single line; use
GlassTextAreafor multiline text. - Validation is synchronous. Asynchronous checks (availability of a name) go through
errorMessage. - Length and custom rules accept an empty value; add
.required()to refuse it. - On macOS, capitalisation and keyboard settings have no effect, and only some content types exist.
- Focus changes and the keyboard were not exercised on a running device here.
Example
import SwiftUI
import SPGlassKit
public struct GlassTextFieldExample: GlassExample {
public static let componentID = "glass-text-field"
@State private var name = ""
@State private var email = ""
@State private var handle = "taken"
@State private var locked = "Read-only value"
@FocusState private var emailFocused: Bool
public init() {}
public var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 20) {
GlassTextField(
"Full name",
text: $name,
prompt: "Ada Lovelace",
validation: GlassFieldValidation(.required(), .minLength(2)),
input: .name
)
.onSubmit { emailFocused = true }
GlassTextField(
"Email",
text: $email,
prompt: "name@example.com",
helper: "Used only for the receipt.",
systemImage: "envelope",
validation: GlassFieldValidation(.required(), .custom(message: "Enter an address such as name@example.com") { $0.contains("@") && $0.contains(".") }),
input: .email,
focus: $emailFocused
)
GlassTextField(
"Username",
text: $handle,
validation: GlassFieldValidation(.maxLength(12)),
validateOn: .always,
errorMessage: handle == "taken" ? "This username is already taken." : nil,
input: .username
)
GlassTextField("Account", text: $locked)
.disabled(true)
Button("Focus the email field") { emailFocused = true }
}
.padding()
}
}
}
#Preview("GlassTextField") { GlassTextFieldExample().glassTheme(.aurora) }
Registry entry
| Identifier | glass-text-field |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-forms-support, glass-foundation |
| Frameworks | SwiftUI |
| Public symbols | GlassTextField |
| Licence | MIT |
| Manifest | manifest.json |
Files
Forms/Forms/GlassTextField.swift6000 bytes · sha256 61b3b6a03fcb…