UIKit

[UIKit CodeSnippets] UITextField Snippet (Only-code)

usia_ 2024. 7. 24. 10:56

Snippets 추가에서 Completion 을 $uitextfield 로 설정

 

 

+ Snippet 추가 설명

 

Xcode 코드 스닛펫, Code snippet

코드 스닛펫(Code snippet)이란 코드의 단편, 조각이란 뜻으로 자동완성기능을 의미한다.애플에서 미리 만들어둔 템플릿도 많으며, 커스텀을 할 수 있다.커스텀을 하면서 사용하면 자주 쓰는 코드

usiacode.tistory.com

 

 

 

  • viewDidLoad()
    • textfield.delegate =  self // 델리게이트 설정
  • extension ViewController: UITextFieldDelegate 를 추가 후 델리게이트 메소드 선택적 추가

 

private lazy var myTextField: UITextField = {
        var tf = UITextField()
        tf.placeholder = "이메일을 입력하세요." // 플레이스홀더 설정
        tf.frame.size.height = 48
        tf.backgroundColor = .clear
        tf.textColor = .black
        tf.tintColor = .black
        tf.autocapitalizationType = .none // 자동대문자
        tf.autocorrectionType = .no
        tf.spellCheckingType = .no
        tf.keyboardType = .emailAddress
        //tf.addTarget(self, action: #selector(textFieldEditingChanged(_:)), for: .editingChanged) // 값이 변할 때마다 해당 함수 호출
        view.addSubview(tf)
        //tf.borderStyle = .roundedRect // 테두리 설정
        //tf.clearButtonMode = .whileEditing // 클리어버튼 설정
        //tf.returnKeyType = .done // 리턴키 설정
        return tf
    }()
    
    /*  viewDidLoad() 에 추가
     // 대리자 설정
     myTextField.delegate =  self
     
     // myTextField.becomeFirstResponder() // 맨 처음 뷰에서 키보드 올라오게 (viewDidLoad)
     
     // 오토레이아웃
     func myTextFieldSetup() {
     myTextField.translatesAutoresizingMaskIntoConstraints = false
     
     NSLayoutConstraint.activate([
     myTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30),
     myTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30),
     myTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 30),
     myTextField.heightAnchor.constraint(equalToConstant: 40)
     ])
     }
     */
    
    
    /* [extension delegate function]
     extension ViewController: UITextFieldDelegate {
     // 텍스트필드의 편집을 시작할 때 호출 (허락)
     func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
     print("텍스트필드의 편집을 시작합니다.")
     return true
     }
     
     // 텍스트필드의 편집이 시작된 후 호출
     func textFieldDidBeginEditing(_ textField: UITextField) {
     print("텍스트필드의 편집이 시작되었습니다.")
     }
     
     // 텍스트필드의 내용이 수정될 때 호출 (허락)
     func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
     print("내용이 \(string)으로 변경되었습니다.")
     if Int(string) == nil {
     return true
     } else {
     return false
     }
     }
     
     // 텍스트필드의 내용이 삭제될 때 호출
     func textFieldShouldClear(_ textField: UITextField) -> Bool {
     print("텍스트필드의 내용이 삭제되었습니다.")
     return true
     }
     
     // 텍스트필드의 리턴키가 눌렸을 때 호출 (엔터 -> 버튼 눌리게)
     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
     print("텍스트필드의 리턴키가 눌렸습니다.")
     myTextField.resignFirstResponder() // 키보드 내려지게 FirstResponder 해제
     return true
     }
     
     // 텍스트필드의 편집이 종료될 때 호출
     func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
     print("텍스트필드의 편집이 종료됩니다.")
     return true
     }
     
     // 텍스트필드의 편집이 종료되었을 때 호출
     func textFieldDidEndEditing(_ textField: UITextField) {
     print("텍스트필드의 편집이 종료되었습니다.")
     }
     
     
     // + 옆의 버튼이 눌렸을 때 키보드 내려가게
     @IBAction func button(_ sender: UIButton) {
     myTextField.resignFirstResponder()
     }
     }
     */