0Pricing
SwiftUI Academy · 课时

键盘类型与占位提示

配置键盘、提示文本和提交行为。

键盘类型与占位提示 是 CoddyKit 上的免费 SwiftUI Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 SwiftUI Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 SwiftUI Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Shape the Input

A bare TextField accepts anything. With a few modifiers you can shape the keyboard, the hint, and what happens on submit. ⌨️

Choosing a Keyboard

The keyboardType modifier picks which on-screen keyboard appears. An email field deserves the email layout, not the full alphabet.

TextField("Email", text: $email)
  .keyboardType(.emailAddress)

Numbers Only

Asking for a phone or amount? Use .numberPad or .decimalPad so users get a clean number keyboard with no letters in the way.

TextField("Age", text: $age)
  .keyboardType(.numberPad)

Placeholder vs Prompt

The first string argument is the placeholder, the gray hint shown while the field is empty. It guides users without taking up extra space.

TextField("Search recipes", text: $query)

A Separate Prompt

You can also pass a prompt as its own argument. This keeps the visible label and the placeholder hint as two distinct pieces of text.

TextField("City", text: $city,
  prompt: Text("e.g. Tokyo"))

Capitalization Control

Use textInputAutocapitalization to decide how the keyboard capitalizes. Set it to .never for usernames so nothing is auto-uppercased.

TextField("Username", text: $user)
  .textInputAutocapitalization(.never)

Turning Off Autocorrect

For codes, handles, or URLs, autocorrect gets in the way. Add autocorrectionDisabled to let users type exactly what they mean.

TextField("Handle", text: $handle)
  .autocorrectionDisabled()

The Return Key Label

The submitLabel modifier changes the keyboard return key text. Use .search, .done, or .next to match what the field actually does.

TextField("Search", text: $query)
  .submitLabel(.search)

Reacting to Submit

The onSubmit modifier runs your code when the user taps return. It is the perfect place to start a search or move focus.

TextField("Search", text: $query)
  .onSubmit { runSearch() }

Stack the Modifiers

These modifiers chain freely. Combine keyboard type, capitalization, and submit label on one field to craft exactly the input you want.

Small Details, Big Polish

The right keyboard and a clear prompt feel effortless to users. These tiny touches are what make an app feel professional. ✨

Quick Check

Which modifier changes the keyboard that appears for a TextField?

Recap

You tuned a field with keyboardType, prompts, capitalization, and onSubmit. Small modifiers turn a plain box into thoughtful, friendly input. 🎉

常见问题解答

「键盘类型与占位提示」课时是免费的吗?

是的 — 「键盘类型与占位提示」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 SwiftUI Academy 课程的其余内容,请升级到 CoddyKit PRO。 SwiftUI Academy 课程共包含 4 节课。

「键盘类型与占位提示」这节课中我会学到什么?

配置键盘、提示文本和提交行为。 你通过在浏览器中直接运行的动手代码来练习 SwiftUI Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 SwiftUI Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 SwiftUI Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「键盘类型与占位提示」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 SwiftUI Academy 课中编写并运行代码吗?

能。每节 SwiftUI Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 将 TextField 绑定到状态
  2. 键盘类型与占位提示
  3. 使用 SecureField 输入密码
  4. 实时输入验证
← 返回 SwiftUI Academy