Skip to content

Instantly share code, notes, and snippets.

@katsuyoshi
Last active February 29, 2024 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsuyoshi/4bd5a33aa14c9a23b96feb1e32fdd270 to your computer and use it in GitHub Desktop.
Save katsuyoshi/4bd5a33aa14c9a23b96feb1e32fdd270 to your computer and use it in GitHub Desktop.
UIKeyboardType
import SwiftUI
struct ContentView: View {
@State var inputText = ""
@State var selected: String?
@State var selectedKeyboardType = UIKeyboardType.default
@FocusState var focused
let types: [String: UIKeyboardType] = [
"default": UIKeyboardType.default,
"asciiCapable": UIKeyboardType.asciiCapable,
"numbersAndPunctuation": UIKeyboardType.numbersAndPunctuation,
"URL": UIKeyboardType.URL,
"numberPad": UIKeyboardType.numberPad,
"phonePad": UIKeyboardType.phonePad,
"namePhonePad": UIKeyboardType.namePhonePad,
"emailAddress": UIKeyboardType.emailAddress,
"decimalPad": UIKeyboardType.decimalPad,
"twitter": UIKeyboardType.twitter,
"webSearch": UIKeyboardType.webSearch,
"asciiCapableNumberPad": UIKeyboardType.asciiCapableNumberPad,
"alphabet": UIKeyboardType.alphabet
]
@State var keys: [String] = []
var body: some View {
VStack {
TextField("test", text: $inputText)
.keyboardType(selectedKeyboardType)
.focused($focused)
List(selection: $selected) {
ForEach(keys, id: \.self) {t in
Text("\(types[t]!.rawValue): \(t)")
.tag(t)
}
}
}
.padding()
.onAppear() {
keys = types.keys.sorted {
types[$0]!.rawValue < types[$1]!.rawValue
}
}
.onChange(of: selected) { oldValue, newValue in
if let newValue {
selectedKeyboardType = types[newValue]!
} else {
selectedKeyboardType = .default
}
focused = false
Task {
try! await Task.sleep(nanoseconds: 300000)
focused = true
}
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment