iOS: Auto-capitalization switch for the full keyboard (#2116)

TestFlight testers (builds 54/55) asked to turn off the capital at the start
of their typing ("you can lowercase everything on dictation but not regular
keyboard"). Settings › Keyboard gains Auto-capitalization, above Autocorrect,
on by default, as Apple's switch of the same name
(FullKeyboardSettings.autoCapitalization, App Group key
settings.keyboardAutoCapitalization; "Capitalizes the start of sentences",
zh-Hans 自动大写 / 句首字母自动大写).

Off, Hex adds no capital you didn't type, matching Apple's keyboard with
Auto-Capitalization off on the iOS 27.2 simulator ("hello world. i am mary.
im here. monday" -> "hello world. i am mary. i'm here. monday"; "^hi i speak
englsh on mondya with ^mary" -> "Hi i speak english on monday with Mary"):
- Shift never turns itself on, whatever the field's autocapitalizationType
  (sentences, words, allCharacters): not at a field's or sentence's start,
  after "." "!" "?", or after the double-space period (which still types
  ". "). FullKeyboardEngine.autoCapitalizes maps the mode to .none.
- Autocorrect (AutocorrectEngine.addsCapitals, passed to bilingual
  autocorrect's partner): English fixes, Spatial readings and UITextChecker
  guesses keep the case you typed ("i" stays, "im" -> "i'm", "mondya" ->
  "monday"); a fix that would only add a capital ("i" -> "I") isn't made.
  #2091's lowercase-name capitalisation ("mary" -> "Mary") is skipped.
- Swipe typing (#2106): no sentence-start capital from the space it adds,
  and no name capital; readings come out lowercase.
Kept: capitals you typed (shift, caps lock, "Teh" -> "The", "Im" -> "I'm"),
and your own spellings: text replacements, Contacts names and other UILexicon
terms, words you kept, fixes Hex learned (isOwnSpelling). The strip still
offers capitalised words to pick. On, nothing changes.
Dictation is separate: Settings › Dictation › Lowercase (#2103) is untouched.

Verified on an iPhone 17 simulator (iOS 27.2), Mac Studio:
- xcodebuild test: 365 tests, 0 failures, 1 skipped (needs
  HEX_SPEECH_CLIPS), including the new cases in FullKeyboardEngineTests
  (shift never turns on in any mode, shift and caps lock work, switching
  back on capitalises; a swiped word after "Great." stays lowercase) and
  AutocorrectEngineTests (fixes add no capitals; Spatial keeps "mary" and
  "monday" lowercase and a Contacts name's capital).
- The Settings switch, tapped, stored false in the App Group.
- Live in Messages with Hex's keyboard, off: an empty message showed
  lowercase keys; "hello world. i think im here. mary is here! ok" typed as
  "hello world. i think i'm here. mary is here! ok", and shift then "ok"
  gave "Ok". On: "Hello world. I think I'm here. Mary is here! Ok", as before.
- Settings › Keyboard rendered in English and Simplified Chinese.
This commit is contained in:
Ryan Vogel
2026-11-20 23:53:33 +00:00
committed by GitHub
parent 68127fde7a
commit 2ef08aa491
10 changed files with 200 additions and 9 deletions
+20
View File
@@ -510,6 +510,16 @@
}
}
},
"Auto-capitalization" : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "自动大写"
}
}
}
},
"Autocorrect" : {
"localizations" : {
"zh-Hans" : {
@@ -560,6 +570,16 @@
}
}
},
"Capitalizes the start of sentences" : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "句首字母自动大写"
}
}
}
},
"Casual" : {
"localizations" : {
"zh-Hans" : {
+1
View File
@@ -264,6 +264,7 @@ enum SettingsSnapshotFixture {
let on = scene.contains("-on")
let keyboard = FullKeyboardSettings()
keyboard.isEnabled = on || opensKeyboardLanguages
keyboard.autoCapitalization = true
keyboard.autocorrect = true
keyboard.spatialAutocorrect = true
keyboard.suggestSwearWords = on
+14 -1
View File
@@ -138,12 +138,13 @@ private enum KeyboardChoice: CaseIterable {
}
/// Settings › Keyboard: Full or Dictation bar, the full keyboard's rows (only while it's
/// chosen; Spatial autocorrect only while Autocorrect is on, and Suggest swear words off by
/// chosen; Auto-capitalization, on by default as Apple's, sits above Autocorrect; Spatial autocorrect only while Autocorrect is on, and Suggest swear words off by
/// default, as Apple's keyboard never offers them), and haptics, which the dictation bar uses
/// too.
struct KeyboardSettingsPage: View {
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@State private var fullKeyboard = FullKeyboardSettings().isEnabled
@State private var autoCapitalization = FullKeyboardSettings().autoCapitalization
@State private var autocorrect = FullKeyboardSettings().autocorrect
@State private var spatialAutocorrect = FullKeyboardSettings().spatialAutocorrect
@State private var suggestSwearWords = FullKeyboardSettings().suggestSwearWords
@@ -174,6 +175,14 @@ struct KeyboardSettingsPage: View {
SettingsDivider()
SettingsToggleRow(
title: String(localized: "Auto-capitalization"),
detail: String(localized: "Capitalizes the start of sentences"),
isOn: $autoCapitalization
)
SettingsDivider()
SettingsToggleRow(
title: String(localized: "Autocorrect"),
detail: String(localized: "Fixes typos when you press space"),
@@ -232,6 +241,9 @@ struct KeyboardSettingsPage: View {
.onChange(of: fullKeyboard) { _, enabled in
FullKeyboardSettings().isEnabled = enabled
}
.onChange(of: autoCapitalization) { _, enabled in
FullKeyboardSettings().autoCapitalization = enabled
}
.onChange(of: autocorrect) { _, enabled in
FullKeyboardSettings().autocorrect = enabled
}
@@ -249,6 +261,7 @@ struct KeyboardSettingsPage: View {
}
.onAppear {
fullKeyboard = FullKeyboardSettings().isEnabled
autoCapitalization = FullKeyboardSettings().autoCapitalization
autocorrect = FullKeyboardSettings().autocorrect
spatialAutocorrect = FullKeyboardSettings().spatialAutocorrect
suggestSwearWords = FullKeyboardSettings().suggestSwearWords
@@ -134,6 +134,32 @@ final class AutocorrectEngineTests: XCTestCase {
}
}
/// Settings › Keyboard › Auto-capitalization off, as Apple's keyboard with it off (iOS 27.2
/// simulator: "i" stays "i", "im" becomes "i'm", "mondya" "monday"): a fix adds no capital
/// you didn't type, but keeps those you typed and your own spellings'.
func testAutoCapitalizationOffAddsNoCapitalsToFixes() {
engine.autoCapitalizes = false
engine.autocorrect.loadLexiconEntries([("omw", "On my way!"), ("Kit", "Kit")])
let replacements: [(String, String?)] = [
("i", nil),
("im", "i'm"),
("Im", "I'm"),
("ive", "i've"),
("dont", "don't"),
("Teh", "The"),
("mondya", "monday"),
("omw", "On my way!"),
]
for (typed, expected) in replacements {
let actual = engine.autocorrect.replacementOnSpace(beforeInput: typed, field: .text, autocorrectionType: .default)
XCTAssertEqual(actual?.replacement, expected, "expected \(typed) -> \(String(describing: expected))")
}
target.autocapitalizationType = .sentences
engine.configureForField()
typeWord("i think im here ")
XCTAssertEqual(target.text, "i think i'm here ")
}
func testAmbiguousContractionsSuggestWithoutAutoReplacing() {
let slots = engine.autocorrect.suggestions(
beforeInput: "well",
@@ -585,6 +611,16 @@ final class AutocorrectEngineTests: XCTestCase {
XCTAssertEqual(target.text, "see Mary on Monday in Boston ")
}
/// Auto-capitalization off: a name typed in lowercase stays lowercase, and a slip in one
/// of your own names still gets its capital.
func testSpatialAutocorrectKeepsNamesLowercaseWithAutoCapitalizationOff() throws {
try useDecoder()
engine.autoCapitalizes = false
engine.autocorrect.loadLexiconEntries([("Zorbix", "Zorbix")])
for word in ["see", "mary", "on", "monday", "with", "zorbiz"] { typeTaps(word) }
XCTAssertEqual(target.text, "see mary on monday with Zorbix ")
}
func testSpatialAutocorrectAddsSpanishAccents() throws {
try useDecoder(.spanish)
for word in ["esta", "aqui", "manana"] { typeTaps(word) }
@@ -224,6 +224,41 @@ final class FullKeyboardEngineTests: XCTestCase {
XCTAssertEqual(target.text, "Hi. There")
}
/// Settings › Keyboard › Auto-capitalization off, as Apple's switch: shift never turns
/// itself on, whatever the field asks, not even after the double-space period; shift and
/// caps lock still work.
func testAutoCapitalizationOffNeverTurnsShiftOn() {
engine.autoCapitalizes = false
for type in [UITextAutocapitalizationType.sentences, .words, .allCharacters] {
target.autocapitalizationType = type
engine.configureForField()
XCTAssertEqual(engine.shift.state, .off, "autocapitalizationType \(type.rawValue)")
}
target.autocapitalizationType = .sentences
engine.configureForField()
type("hi")
tap(.space)
now += 0.2
tap(.space)
XCTAssertEqual(engine.shift.state, .off, "after the double-space period")
type("there ok")
tap(.shift)
type("x")
tap(.shift)
tap(.shift)
type("yz")
XCTAssertEqual(target.text, "hi. there okXYZ", "shift, then caps lock")
tap(.shift)
tap(.space)
now += 0.2
tap(.space)
XCTAssertEqual(target.text, "hi. there okXYZ. ")
XCTAssertEqual(engine.shift.state, .off)
engine.autoCapitalizes = true
XCTAssertEqual(engine.shift.state, .on, "switched back on, the next sentence starts with a capital")
}
func testAcceleratingDeleteRampAndWordDelete() {
type("hello world")
down(1, .delete)
@@ -1008,6 +1043,19 @@ final class FullKeyboardEngineTests: XCTestCase {
XCTAssertEqual(locked.text, "PEOPLE", "caps lock")
}
/// Auto-capitalization off: no sentence-start capital on a swiped word, as on Apple's
/// keyboard with it off; shift still gives one.
func testAutoCapitalizationOffLeavesASwipedWordLowercase() throws {
let (sentence, keys) = try swipeKeyboard("Great.")
sentence.autocapitalizationType = .sentences
keys.engine.autoCapitalizes = false
swipe("thanks", on: keys)
XCTAssertEqual(sentence.text, "Great. thanks", "the space the swipe adds starts no sentence")
tap(.shift, on: keys)
swipe("hello", on: keys)
XCTAssertEqual(sentence.text, "Great. thanks Hello", "shift still capitalises")
}
func testDeleteRightAfterASwipeTakesTheWholeWordBack() throws {
let (field, plane) = try swipeKeyboard()
swipe("hello", on: plane)
@@ -252,6 +252,13 @@ final class AutocorrectEngine {
partner?.allowsSwearing = allowsSwearing
}
}
/// Settings › Keyboard › Auto-capitalization. Off, a fix from the dictionary or Spatial
/// autocorrect's model adds no capital you didn't type, as Apple's keyboard with
/// Auto-Capitalization off: "i" stays "i", "im" becomes "i'm", "mary" stays "mary" and
/// "mondya" becomes "monday". Your own spellings keep theirs (`isOwnSpelling`). On by default.
var addsCapitals = true {
didSet { partner?.addsCapitals = addsCapitals }
}
private var decoderCache: (before: String, taps: [CGPoint], decision: TypingDecoder.Decision?)?
init(
@@ -359,6 +366,7 @@ final class AutocorrectEngine {
engine.lexiconTerms = lexiconTerms
engine.learning = learning
engine.allowsSwearing = allowsSwearing
engine.addsCapitals = addsCapitals
engine.decoderKeys = decoderKeys
engine.decoderTaps = decoderTaps
partner = engine
@@ -654,7 +662,7 @@ final class AutocorrectEngine {
// Norwegian "i" means "in" and must not become "I").
if language.usesEnglishFixes, let fixed = Self.unambiguousFixes[lower] {
if isAllCaps && token.count < 3 { return nil }
return Self.matchCase(of: token, onto: fixed)
return withoutAddedCapitals(Self.matchCase(of: token, onto: fixed), typed: token)
}
if isAllCaps { return nil }
@@ -662,11 +670,12 @@ final class AutocorrectEngine {
// Spatial autocorrect, when on, decides alone: your taps, the word before, and a margin.
if let decision = decoderDecision(for: token) {
if let replacement = decision.replacement, mayOffer(replacement, replacing: token) {
return Self.matchCase(of: token, onto: replacement)
return withoutAddedCapitals(Self.matchCase(of: token, onto: replacement), typed: token)
}
// A name, day or place its lowercase word list lacks, typed in lowercase, is
// capitalised as the dictionary path does ("mary" -> "Mary").
return properNoun(for: token)
// capitalised as the dictionary path does ("mary" -> "Mary"), unless
// Auto-capitalization is off.
return addsCapitals ? properNoun(for: token) : nil
}
// 3. A two-letter non-word one neighbouring key from a very common two-letter word
@@ -694,7 +703,24 @@ final class AutocorrectEngine {
}
}
guard let bestGuess, bestScore <= 1.35 else { return nil }
return Self.matchCase(of: token, onto: bestGuess)
return withoutAddedCapitals(Self.matchCase(of: token, onto: bestGuess), typed: token)
}
/// `fix` for `token` as autocorrect puts it in: with `addsCapitals` off and nothing
/// typed in capitals, all lowercase unless it is your own spelling; nil when that leaves
/// just what you typed ("i").
private func withoutAddedCapitals(_ fix: String, typed token: String) -> String? {
guard !addsCapitals, !token.contains(where: \.isUppercase), !isOwnSpelling(fix) else { return fix }
let lowered = fix.lowercased()
return lowered == token ? nil : lowered
}
/// Whether `word`, capitals and all, is one of your own spellings: a Contacts name or
/// other `UILexicon` term, a text replacement's output, a word you kept twice, or a fix
/// Hex learned from you. Auto-capitalization off leaves their capitals alone.
func isOwnSpelling(_ word: String) -> Bool {
lexiconTerms.contains(word) || lexiconShortcuts.values.contains(word)
|| learning.typos.values.contains(word) || learnedWords.words.contains(word)
}
/// The very common two-letter word (the language's `commonTwoLetterWords`, commonest
@@ -219,6 +219,23 @@ final class FullKeyboardEngine {
/// Settings › Key pop-ups. Off: pressed keys only darken, as with iOS's Character Preview off.
var showsKeyPopups = true
/// Settings › Keyboard › Auto-capitalization. Off, shift never turns itself on, whatever
/// the field's `autocapitalizationType` asks, and autocorrect and swipe typing add no
/// capitals (`AutocorrectEngine.addsCapitals`). Shift and caps lock still work.
var autoCapitalizes = true {
didSet {
guard autoCapitalizes != oldValue else { return }
autocorrect.addsCapitals = autoCapitalizes
refreshAutoCapitalization()
refreshSuggestions()
}
}
/// What the field asks for, or no automatic capitals with Auto-capitalization off.
private var autoCapitalizationMode: AutoCapitalization.Mode {
autoCapitalizes ? AutoCapitalization.Mode(target.autocapitalizationType) : .none
}
/// The key whose balloon shows: the newest character still held, or the one a 123, #+= or
/// shift press slid onto (hidden while an accent menu is up).
var previewKey: KeyboardKey? {
@@ -284,7 +301,7 @@ final class FullKeyboardEngine {
let before = shift
shift.refreshAutoCapitalization(
AutoCapitalization.shouldCapitalize(
mode: AutoCapitalization.Mode(target.autocapitalizationType),
mode: autoCapitalizationMode,
before: target.documentContextBeforeInput,
hasText: target.hasText
)
@@ -1152,7 +1169,7 @@ final class FullKeyboardEngine {
let before = target.documentContextBeforeInput
guard SwipeTyping.needsSpace(before: before) else { return false }
return AutoCapitalization.shouldCapitalize(
mode: AutoCapitalization.Mode(target.autocapitalizationType),
mode: autoCapitalizationMode,
before: (before ?? "") + " ",
hasText: true
)
@@ -1160,10 +1177,15 @@ final class FullKeyboardEngine {
/// A reading as it's typed: all capitals with caps lock, a capital first letter with
/// `capital`, else a name the dictionary only knows capitalised ("monday" -> "Monday").
/// With Auto-capitalization off, no capital you didn't ask for ("i'm", "monday"), but
/// your own spellings keep theirs.
private func swipeCased(_ word: String, capital: Bool) -> String {
if shift.state == .locked { return word.uppercased() }
guard let first = word.first else { return word }
if capital { return first.uppercased() + word.dropFirst() }
if !autoCapitalizes {
return autocorrect.isOwnSpelling(word) ? word : word.lowercased()
}
guard autocorrect.hasDictionary, word == word.lowercased(), word.count >= 2 else { return word }
let capitalised = first.uppercased() + word.dropFirst()
return autocorrect.checker.isWordValid(capitalised) && !autocorrect.checker.isWordValid(word) ? capitalised : word
@@ -16,6 +16,7 @@ struct FullKeyboardSettings {
private enum Key {
static let fullKeyboard = "experiments.fullKeyboard"
static let autocorrect = "settings.keyboardAutocorrect"
static let autoCapitalization = "settings.keyboardAutoCapitalization"
static let spatialAutocorrect = "experiments.spatialAutocorrect"
static let keyPopups = "settings.keyboardKeyPopups"
static let haptics = "settings.keyboardHaptics"
@@ -41,6 +42,17 @@ struct FullKeyboardSettings {
nonmutating set { defaults?.set(newValue, forKey: Key.fullKeyboard) }
}
/// Settings › Keyboard › Auto-capitalization, as Apple's switch of the same name. On by
/// default: shift turns itself on where the field asks (the start of a field or sentence,
/// after "." "!" "?"). Off, Hex never adds a capital you didn't type: shift stays off
/// whatever the field's `autocapitalizationType`, and autocorrect and swipe typing keep
/// words lowercase ("i", "i'm", "monday") unless they're your own spelling. Dictation
/// keeps its own Lowercase setting.
var autoCapitalization: Bool {
get { defaults?.object(forKey: Key.autoCapitalization) as? Bool ?? true }
nonmutating set { defaults?.set(newValue, forKey: Key.autoCapitalization) }
}
/// Cautious autocorrect on space. On by default.
var autocorrect: Bool {
get { defaults?.object(forKey: Key.autocorrect) as? Bool ?? true }
@@ -169,6 +169,7 @@ final class FullKeyboardView: UIView, UIInputViewAudioFeedback {
engine.autocorrect.isEnabled = FullKeyboardSettings().autocorrect
engine.autocorrect.allowsSwearing = FullKeyboardSettings().suggestSwearWords
engine.autoCapitalizes = FullKeyboardSettings().autoCapitalization
engine.showsKeyPopups = FullKeyboardSettings().keyPopups
refreshEmojiKey()
syncSpatialAutocorrect()
@@ -370,6 +371,7 @@ final class FullKeyboardView: UIView, UIInputViewAudioFeedback {
engine.journal = journal
}
engine.showsKeyPopups = FullKeyboardSettings().keyPopups
engine.autoCapitalizes = FullKeyboardSettings().autoCapitalization
refreshEmojiKey()
let wantsAutocorrect = FullKeyboardSettings().autocorrect
if engine.autocorrect.isEnabled != wantsAutocorrect {
+12 -1
View File
@@ -251,6 +251,17 @@ doesn't use, and `SpeechAnalyzer` runs without it in the app and the keyboard al
Typing insights lists these fixes with a switch each and the share of
dictations kept and edited; Delete what Hex learned erases everything,
including what a running keyboard still holds.
- **Auto-capitalization (Settings › Keyboard › Auto-capitalization):** On by default,
as Apple's (`FullKeyboardSettings.autoCapitalization`, App Group key
`settings.keyboardAutoCapitalization`). Off, shift never turns itself on, whatever the
field's `autocapitalizationType` asks (a field's or sentence's start, after "." "!" "?"
or the double-space period, which still types ". "), and autocorrect and swipe typing add no
capital you didn't type: "i" stays "i", "im" becomes "i'm", "mary" stays "mary" and
"mondya" becomes "monday", as Apple's keyboard does with Auto-Capitalization off (seen on
the iOS 27.2 simulator). Words you typed with a capital, and your own spellings (text
replacements, Contacts names, words you kept, fixes Hex learned), keep theirs; shift and
caps lock work as before, and the strip may still offer a capitalised word to pick.
Dictation has its own switch (Settings › Dictation › Lowercase).
- **Spatial autocorrect (Settings › Keyboard › Spatial autocorrect):**
On by default, shown under Autocorrect while it's on (it needs the full keyboard
with Autocorrect on). Stored as `experiments.spatialAutocorrect` from when it was an
@@ -363,7 +374,7 @@ doesn't use, and `SpeechAnalyzer` runs without it in the app and the keyboard al
as `FullKeyboardSettings.isEnabled`, App Group key `experiments.fullKeyboard`
from when the full keyboard was an experiment: unset or true is Full, false
the dictation bar, so testers who turned that experiment off keep the bar),
then Languages, Autocorrect (with Spatial autocorrect below it while it's
then Languages, Auto-capitalization, Autocorrect (with Spatial autocorrect below it while it's
on), Suggest swear words and Key pop-ups only while Full is chosen, Haptics (the dictation bar uses it too),
and a field to try the keyboard in.
- **Privacy:** "Everything stays on this iPhone", Learn how I type (what it