๐จํ๋ฆฐ ๋ถ๋ถ์ด ์์ ์๋ ์์ต๋๋ค๐จ
ํ
์คํธํ๋ ์ฌ์ฉ๋ฒ
textField.keyboardType = UIKeyboardType.emailAddress // ํ
์คํธํ๋์ ํค๋ณด๋ ์คํ์ผ
textField.placeholder = "์ด๋ฉ์ผ์ ์
๋ ฅํ์ธ์" // ํ
์คํธํ๋ ํ๋ ์ด์คํ๋
textField.borderStyle = .roundedRect // ํ
์คํธํ๋ ์ ์คํ์ผ
textField.clearButtonMode = .always // ๋ฌธ์ ํ ๋ฒ์ ์ง์ฐ๋ ๋ฒํผ ํ์ฑ/๋นํ์ฑ
textField.returnKeyType = .go // ํค๋ณด๋ ์ ์ผ ์ค๋ฅธ์ชฝ ๋ฐ์ ํ์ธ ๋ฒํผ ๋ณ๊ฒฝ
textField.becomeFirstResponder() // ์ด ํ๋ฉด์ด ๋ฐ๋ ํ
์คํธํ๋๋ฅผ ๊ฐ์ฅ ์ฒ์์ผ๋ก ๋ฐ์ํ๊ฒ ๋ง๋ค์ด์ ํค๋ณด๋๋ฅผ ์๋์ผ๋ก ์ฌ๋ผ์ค๊ฒ ํจ
textField.resignFirstResponder() // ํค๋ณด๋ ๋ด๋ฆฌ๊ธฐ
textField.autocapitalizationType = .none // ์๋์ผ๋ก ์ฒซ ๋ฌธ์ ๋๋ฌธ์ ์ค์
textField.autocorrectionType = .no // ์๋์ผ๋ก ํ๋ฆฐ ๊ธ์ ์์
textField.spellCheckingType = .no // ํ๋ฆฐ ๊ธ์ ์ฐพ๊ธฐ
textField.isSecureTextEntry = true // ๋น๋ฐ๋ฒํธ ๊ฐ๋ฆฌ๋ ์ค์
ํ
์คํธํ๋์์ ๊ธ์์๋ฅผ ์ ํํ๋ ๋ฐฉ๋ฒ
// ํ๊ธ์ ํ๊ธ์ ์
๋ ฅํ ๋๋ง๋ค ํธ์ถ -> ์ฐธ(์
๋ ฅํ๋ ๊ฒ์ ํ๋ฝ) / ๊ฑฐ์ง(์
๋ ฅํ๋ ๊ฒ์ ๊ฑฐ๋ถ)
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// ์
๋ ฅ๋๊ณ ์๋ ๊ธ์๊ฐ "์ซ์"์ธ ๊ฒฝ์ฐ ์
๋ ฅ์ ํ์ฉํ์ง ์๋ ๋
ผ๋ฆฌ
if Int(string) != nil { // (์ซ์๋ก ๋ณํ์ด ๋๋ค๋ฉด nil์ด ์๋ํ
๋)
return false
} else {
// 10๊ธ์์ด์ ์
๋ ฅ๋๋ ๊ฒ์ ๋ง๋ ์ฝ๋
guard let text = textField.text else { return true }
let newLength = text.count + string.count - range.length
return newLength <= 10
}
// ์ซ์๋ง ์
๋ ฅ๊ฐ๋ฅํ๊ฒ ์ค์
if Int(string) == nil || string == "" {
return true
}
return false
}
ํ๋ฉดํฐ์น ๊ฐ์ง(ํค๋ณด๋)
// ํ๋ฉด ํฐ์น๋ฅผ ํ ๋ ํธ์ถ
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// ํ๋ฉด ํฐ์น๊ฐ ์ผ์ด๋๋ฉด ํค๋ณด๋๋ฅผ ๋ด๋ฆผ
self.view.endEditing(true)
}
View๋ฅผ ํ๋ฉด์ ๋ํ๋ด๊ธฐ(์ฝ๋)
// ๋ฉ๋ชจ๋ฆฌ์ UIView๋ฅผ ์ฌ๋ ค๋๊ธฐ
let emailTextFieldView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
// ๊ธฐ๋ณธ view ์์ ๋ด๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ ค๋์ view๋ฅผ ์ฌ๋ ค์ ๋ํ๋ด๊ธฐ
view.addSubview(emailTextFieldView)
}
์คํ ๋ ์ด์์(์ฝ๋)
// ์๋์ผ๋ก ์คํ ๋ ์ด์์์ ์ก๋ ๊ธฐ๋ฅ์ ๋ (๋ฐ๋์ ์ค์ ํด์ผ ์คํ ๋ ์ด์์์ ์๋์ผ๋ก ์ ์ฉ์ํฌ ์ ์์)
emailTextFieldView.translatesAutoresizingMaskIntoConstraints = false
// left๋ ์ผ์ชฝ leading์ ๊ธ์ ์ฝ๋ ๋ฐฉํฅ (์๋์ชฝ์ ์ค๋ฅธ์ชฝ์ด leading)
emailTextFieldView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30).isActive = true
// ์ค๋ฅธ์ชฝ(์์น ์์ '-' ๋ถํ์ผํจ<ํ๋ฉด ์์ผ๋ก ๋ค์ด์ฌ์์>)
emailTextFieldView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
// ์
emailTextFieldView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
// ๋์ด
emailTextFieldView.heightAnchor.constraint(equalToConstant: 40).isActive = true
// safeArea๊ธฐ์ค ์ผ์ชฝ
emailTextFieldView.safeAreaLayoutGuide.leadingAnchor
// isActive๋ฅผ ์ ๊ธฐ ๊ท์ฐฎ์ผ๋ฉด ์ด๋ฐ์์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ
NSLayoutConstraint.activate([
emailInfoLabel.leadingAnchor.constraint(equalTo: emailTextFieldView.leadingAnchor, constant: 8),
emailInfoLabel.trailingAnchor.constraint(equalTo: emailTextFieldView.trailingAnchor, constant: -8),
emailInfoLabel.centerYAnchor.constraint(equalTo: emailTextFieldView.centerYAnchor)
])
๋ทฐ๋ฅผ ๋ฅ๊ธ๊ฒ ํ๋ ๋ฒ
// ์์น๊ฐ ์ฌ๋ผ๊ฐ๋ฉด ๋ ๋ฅ๊ธ๊ฒ ๋ฐ๋
emailTextFieldView.layer.cornerRadius = 8
emailTextFieldView.layer.masksToBounds = true
ํด๋ก์ ์ ์คํ๋ฌธํ์
์ผ๋ก ์ค์