-
-
Save katsuyoshi/4bd5a33aa14c9a23b96feb1e32fdd270 to your computer and use it in GitHub Desktop.
UIKeyboardType
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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