TextField로 문자열을 입력받는 방법과 포커스를 제어하는 방법
@State private var email: String = ""
...
var body: some View {
Form {
Section {
//title, 입력값저장변수, placeholder
TextField("Email", text: $email, prompt: Text("Input Email"))
}
}
}
TextField("Email", text: $email, prompt: Text("Input Email"))
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
SecureField("password", text: $password, prompt: Text("Input Password"))
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
@FocusState private var emailFocused: Bool
var body: some View {
Form {
Section {
//title, 입력값저장변수, placeholder
TextField("Email", text: $email, prompt: Text("Input Email"))
.focused($emailFocused)
}
}
}
//포커스를 설정하는 부분
Button {
emailFocused = true
} label: {
...
}
.submitLabel()
: 엔터키의 이름 변경.onSubmit{}
: 엔터키 작동 방식 설정@State private var email: String = ""
@State private var password: String = ""
@FocusState private var emailFocused: Bool
@FocusState private var passwordFocused: Bool
...
var body: some View {
Form {
Section {
//title, 입력값저장변수, placeholder
TextField("Email", text: $email, prompt: Text("Input Email"))
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.submitLabel(.next)
.onSubmit {
passwordFocused = true
}
SecureField("password", text: $password, prompt: Text("Input Password"))
.textInputAutocapitalization(.never)
.disableAutocorrection(true)
.submitLabel(.done)
.onSubmit {
//제출 버튼 누르는 것과 동일한 작업
}
}
}
}
enum FieldType: Hashable {
case email
case password
}
//포커스 해제 시 nil
@FocusState private var focusedField: FieldType?
TextField("Email", text: $email, prompt: Text("Input Email"))
.focused($focusedField, equals: .email)
...
SecureField("Password", text: $password, prompt: Text("Input password"))
.focused($focusedField, equals: .password)
var body: some View {
Form {
}
.onAppear {
DispatchQueu.main.asyncAfter(deadline: .now() + 0.1) {
focusedField = .email
}
}
}
TexField(...)
.textFieldStyle(.roundedBorder)
//계정인 경우
TexField(...)
.textContentType(.username)
TexField(...)
.keyboardType(.emailAddress)
.dateTime
등 여러가지 존재TextField("Number", value: $number, format: .number, prompt: Text("0~10"))