GlassOTPField
One logical one-time-code field with paste, AutoFill and a segmented display.
- Content
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-otp-field
Content on an opaque, readable surface. Glass is kept for controls.
CaptureInstallation
spglass add glass-otp-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-otp-field |
|---|---|
| Module | SPGlassForms |
| Kind | content-component — one system TextField under a row of opaque boxes |
| Platforms | iOS 26, iPadOS 26, macOS 26 |
| Example | Examples/Gallery/Forms/GlassOTPFieldExample.swift |
Minimal usage
@State private var code = ""
GlassOTPField(code: $code) { code in
verify(code)
}Variants
| Variant | Use |
|---|---|
length: 4 … 8 | Number of digits. Six by default. |
hasError: true | Outlines the boxes in the destructive colour after a rejected code. |
label: | Accessibility label when "Verification code" is not the right name (a PIN, for example). |
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
code | Binding<String> | — | Digits entered so far, ASCII only. |
length | Int | 6 | Number of digits. |
label | LocalizedStringKey? | "Verification code" | Accessibility label. |
hasError | Bool | false | Error outline. |
onComplete | @MainActor (String) -> Void | { _ in } | Called each time the code becomes complete. |
States
Empty, partially filled (the box that takes the next digit is underlined; while the field has focus it shows a caret and the focus ring), complete (onComplete fires), error (digits and outlines in destructive), disabled. Clearing the binding (code = "") resets the field.
Codes of six or eight digits are drawn as two groups separated by a short dash ("428 – 913"); GlassOTPLayout.groupBoundary(length:) gives the index where the second group starts. The field never shows a placeholder: its label is for assistive technologies only (in 1.0 the label was drawn across the boxes).
Advanced example
GlassOTPField(code: $code, length: 6, hasError: model.codeRejected) { entered in
Task { await model.verify(entered) }
}
.onChange(of: code) { _, _ in model.codeRejected = false }
.disabled(model.isVerifying)Input is cleaned by the pure GlassOTPSanitizer.sanitize(_:length:):
GlassOTPSanitizer.sanitize("Your code is 482 913.", length: 6) // "482913"
GlassOTPSanitizer.sanitize("١٢٣٤٥٦", length: 6) // "123456"Accessibility
- There is one text field, so VoiceOver meets one element: its label and the value "3 of 6 digits entered". The boxes are hidden from assistive technologies and the digits are not read one box at a time by GlassKit.
- The field uses the one-time-code content type, so the system can offer the code received by message, and the number pad on iOS.
- Pasting a whole code works as in any text field: long-press or Command-V; separators and letters are dropped.
- Boxes share the available width up to a square, their height follows Dynamic Type, and the digits shrink rather than clip.
- The caret of the active box blinks, except with Reduce Motion.
- Digits are always laid out left to right, including in right-to-left languages.
Limits
- Digits only; alphanumeric codes are not supported.
- The code lives only in the host's binding. The component stores, logs and copies nothing; clear the binding when you are done.
- The text field is drawn transparent over the boxes, so the system caret and selection highlight are not visible; the outlined box shows the position instead.
- Digits of other scripts are converted to ASCII; symbols that only look like numbers (fractions, Roman numerals, circled digits) are dropped.
- AutoFill from Messages, the paste menu and VoiceOver output need a device and were not exercised here.
Example
import SwiftUI
import SPGlassKit
public struct GlassOTPFieldExample: GlassExample {
public static let componentID = "glass-otp-field"
/// The demo accepts this fictitious code only.
private static let expected = "246810"
@State private var code = ""
@State private var shortCode = ""
@State private var status = "Enter the 6-digit demo code 246810."
@State private var rejected = false
public init() {}
public var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 20) {
GlassOTPField(code: $code, hasError: rejected) { entered in
rejected = entered != Self.expected
status = rejected ? "That code is not valid. Try again." : "Code accepted."
}
.onChange(of: code) { _, newValue in
if newValue.count < 6 { rejected = false }
}
Label(status, systemImage: rejected ? "xmark.octagon" : "info.circle")
.font(.callout)
.foregroundStyle(.secondary)
HStack {
Button("Simulate paste “246 810”") { code = GlassOTPSanitizer.sanitize("246 810", length: 6) }
Button("Clear") { code = "" }
}
Text("Four digits").font(.headline)
GlassOTPField(code: $shortCode, length: 4, label: "PIN")
Text("Disabled").font(.headline)
GlassOTPField(code: .constant("12"), length: 4)
.disabled(true)
}
.padding()
}
}
}
#Preview("GlassOTPField") { GlassOTPFieldExample() }
Registry entry
| Identifier | glass-otp-field |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-forms-support, glass-foundation |
| Frameworks | SwiftUI |
| Public symbols | GlassOTPSanitizer, GlassOTPField, GlassOTPLayout |
| Licence | MIT |
| Manifest | manifest.json |
Files
Forms/Forms/GlassOTPField.swift10036 bytes · sha256 9938fba3ade1…