๐TextField
์ธํฐํ์ด์ค์ ํธ์ง ๊ฐ๋ฅํ ํ ์คํธ ์์ญ์ ํ์ํ๋ ๊ฐ์ฒด
๐TextField์ ๋ค์ํ ์์ฑ๋ค
๐ํค๋ณด๋ ์ข ๋ฅ
textField.keyboardType = .default
textField.keyboardType = .URL
textField.keyboardType = .numberPad
textField.keyboardType = .phonePad
textField.keyboardType = .emailAddress
textField.keyboardType = .webSearch๐return Key ์ข ๋ฅ
textField.returnKeyType = .default
textField.returnKeyType = .done
textField.returnKeyType = .go
textField.returnKeyType = .join
textField.returnKeyType = .next๐๊ธฐํ ์์ ์ ๋ณด
textField.placeholder = "์ด๋ฉ์ผ ์
๋ ฅ" // ๋ค๋ฅธ ํ
์คํธ๊ฐ ์์ ๋ ํ์๋๋ ๋ฌธ์์ด(ํฌ๋ฏธํ ์๋ด ๋ฌธ๊ตฌ)
textField.autocapitalizationType = .none // ์๋์ผ๋ก ์๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ๋ณ๊ฒฝ
textField.autocorrectionType = .no // ์๋ ์์ ํ์ฑํ
textField.spellCheckingType = .no // ๋ง์ถค๋ฒ ๊ฒ์ฌ ํ์ฑํ
textField.clearsOnBeginEditing = false // ํ
์คํธํ๋ ํด๋ฆญ ์ ์ด๋ฏธ ์ ํ์๋ ๋ด์ฉ ์ญ์
textField.isSecureTextEntry.toggle() // ํ
์คํธ๋ฅผ ๋น๋ฐ๋ฒํธ ํ์์ผ๋ก ํ
์คํธ๊ฐ ๋ณด์ด์ง ์๋๋ก ํจ
textField.clearButtonMode = .always // ์ ์ฒด ๋ด์ฉ ์ญ์ ํ "x ๋ฒํผ" ํ์๐TextField์ ๋ธ๋ฆฌ๊ฒ์ดํธ ํจํด
method์ return๊ฐ์ด Bool์ด ์๋ ๊ฒฝ์ฐ๋ ๋ณดํต ์ด๋ ํ ์์ ์ ์๋ฏธ
๐์ ๋ ฅ์ ์์ํ ์ง๋ง์ง ์ฌ๋ถ๋ฅผ ํ๋ฝํ๋ func
// textField์ ์
๋ ฅ์ ์์ํ ๋ ํธ์ถ๋จ
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}๐ํ ์คํธํ๋์ ์ ๋ ฅ์ด ์์๋ ์์ ์ ์๋ ค์ฃผ๋ func
// ์ ์ ๊ฐ ์
๋ ฅ์ ์์ํ ์์ ์ ํธ์ถ๋จ
func textFieldDidBeginEditing(_ textField: UITextField) {
}๐ํ ์คํธํ๋์ ๊ธ์์ ์ ๋ ฅ/์ญ์ ๋ฅผ ํ๋ฝํ๋ func
๋ฌธ์ ์ ๋ ฅ์ ์ ํ์ ๋๋ ์ํฉ์ ์์ฃผ ์ฌ์ฉ
// ํ
์คํธํ๋์ ๊ธ์๊ฐ ์
๋ ฅ๋๊ฑฐ๋ ์ง์์ง ๋ ํธ์ถ๋จ
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return true
}๐ํ ์คํธํ๋์์ return key๊ฐ ๋๋ฌ์ง ํ ๋ค์ ๋์์ ํ๋ฝํ๋ func
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// ํ
์คํธ๊ฐ ์์ ๊ฒฝ์ฐ ํค๋ณด๋ ๋ด๋ ค๊ฐ
if textField.text != "" {
textField.resignFirstResponder()
return true
}
return false
}๐ํ ์คํธํ๋์ ์ ๋ ฅ์ ๋๋ผ์ง ๋ง์ง๋ฅผ ํ๋ฝํ๋ func
// textField์ ์
๋ ฅ์ด ๋๋ ๋ ํธ์ถ
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return true
}๐ํ ์คํธํ๋์ ์ ๋ ฅ์ด ๋๋ ์์ ์ ์๋ ค์ฃผ๋ func
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
print("์ ์ ๊ฐ ์
๋ ฅ ๋๋")
textField.text = ""
}๐TextField์ First Responder
๐ํ ์คํธํ๋๋ฅผ FirstResponder๋ก ์ค์ /ํด์ง
textField.becomeFirstResponder() // ์ค์
textField.resignFirstResponder() // ํด์งFirst Responder๋ก ์ง์ ๋๋ ๋์๊ณผ์
- TextField ํด๋ฆญ
- TextField Delegate๊ฐ
textFieldShouldBeginEditing()ํธ์ถ textFieldShouldBeginEditing()๊ฐ true ๋ฆฌํด (ํ ์คํธ ํ๋์ ์ ๋ ฅ ํ์ฉ)- TextField๊ฐ First Responder๋ก ์ง์ ๋จ
- UIKit์ด TextField์ Input View๋ฅผ ํ๋ฉด์ ํ์ํ๋ ค๊ณ ์๋(Input View : ์ ๋ ฅ์ ๋ฐ๋ ๋ทฐ. ์ฆ, ํค๋ณด๋)
- ํค๋ณด๋ ํ์
First Responder๋ก์์ ์๊ฒฉ ํฌ๊ธฐํ๋ค๋ฉด?
Input View์ด ํ๋ฉด์์ ์ฌ๋ผ์ง๊ฒ ๋จ ํค๋ณด๋์ผ ํ๋ฉด์์ ์๋ ~!!
ํ๋ฉด์ ๋ค์ด๊ฐ์๋ง์ ํ ์คํธํ๋์ ํค๋ณด๋ ์ฌ๋ผ๊ฐ๋๋ก ๊ตฌํ
override func viewDidLoad() {
textField.becomeFirstResponder()
}ํ ์คํธํ๋ ์์ญ ์ธ์ ์์ญ ํฐ์น ์ ํ ์คํธํ๋์ ํค๋ณด๋ ๋ด๋ ค๊ฐ๋๋ก ๊ตฌํ
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// 1๋ฒ ๋ฐฉ๋ฒ
self.view.endEditing(true)
// 2๋ฒ ๋ฐฉ๋ฒ
textField.resignFirstResponder()
}