Skip to content

Latest commit

ย 

History

History
48 lines (36 loc) ยท 1.66 KB

TextField.md

File metadata and controls

48 lines (36 loc) ยท 1.66 KB

TextField

๊ณต์‹๋ฌธ์„œ : A control that displays an editable text interface.

์‚ฌ์šฉ์ž๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ํŽธ์ง‘ํ•  ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ปจํŠธ๋กค์ด๋‹ค.

์‰ฝ๊ฒŒ ๋งํ•ด ์‚ฌ์šฉ์ž๊ฐ€ ํ…์ŠคํŠธ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ทฐ์ธ ๊ฒƒ์ด๋‹ค.

Declaration

@MainActor @preconcurrency
struct TextField<Label> where Label : View

Overview

@State private var username: String = ""
@FocusState private var emailFieldIsFocused: Bool = false


var body: some View {
    TextField(
        "User name (email address)",
        text: $username
    )
    .focused($emailFieldIsFocused)
    .onSubmit {
        validate(name: username)
    }
    .textInputAutocapitalization(.never)
    .disableAutocorrection(true)
    .border(.secondary)


    Text(username)
        .foregroundColor(emailFieldIsFocused ? .red : .blue)
}

TextField๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ํ…์ŠคํŠธ๊ฐ€ ์—†์„ ๋•Œ ๋‚˜ํƒ€๋‚˜๋Š” ํ”Œ๋ ˆ์ด์Šคํ™€๋”๋ฅผ String๊ฐ’์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์œ„์—์„œ ๋ณด์ด๋Š” username๊ณผ ๊ฐ™์ด ๋ฐ”์ธ๋”ฉ ๋œ state ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

๋ฐ”์ธ๋”ฉ์œผ๋กœ ๋ฐ›์€ username์—๋Š” ํ…์ŠคํŠธํ•„๋“œ์— ์ž…๋ ฅํ•œ ๊ฐ’์ด ์ €์žฅ๋˜๋ฉฐ, ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ ๋‹ค๋ฅธ Text๋ทฐ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„ ์˜ˆ์‹œ์—์„œ๋Š” .focused() modifier๋ฅผ ์ด์šฉํ•ด์„œ ํ…์ŠคํŠธ ํ•„๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ์žˆ๊ฑฐ๋‚˜ ์•„๋‹ ๋•Œ๋ฅผ ๊ตฌ๋ถ„ํ•ด Text์˜ ์ƒ‰์ƒ์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์žˆ๋‹ค.

๋˜ํ•œ onSubmit modifier๋Š” ์ˆ˜์ •์„ ์™„๋ฃŒ ํ–ˆ์„ ๋•Œ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์œ„ ์˜ˆ์‹œ์—์„  validate(name: username) ๋ฉ”์†Œ๋“œ๋ฅผ ํ…์ŠคํŠธํ•„๋“œ ์ž…๋ ฅ์„ ์™„๋ฃŒํ–ˆ์„ ๋•Œ ์‹คํ–‰ํ•˜๊ฒŒ๋œ๋‹ค.