텍스트 필드 사용법
textField.keyboardType = UIKeyboardType.numberPad //텍스트필드의 키보드 스타일
textField.placeholder = "이메일 입력"
textField.borderStyle = .roundedRect //텍스트필드의 선 스타일
// 밑줄긋기
let attributedString = NSMutableAttributedString.init(string: "Apply UnderLining")
attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 1, range: NSRange.init(location: 0, length: attributedString.length))
label.attributedText = attributedString
텍스트 필드에서 글자수를 제한하는 방법
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// print(#function)
// print(string)
//let maxLength = 10
//let currentString: NSString = (textField.text ?? "") as NSString
//let newString: NSString =
//currentString.replacingCharacters(in: range, with: string) as NSString
//return newString.length <= maxLength
//다른방법: 입력되고 있는 글자가 "숫자"인 경우 입력을 허용하지 않는 논리
//한글자 한글자 입력시 호출 ==> 참(입력하는것을 허락)/거짓(입력하는 것을 거부)
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
}
}
왼쪽,오른쪽, 탑, 높이 오토레이아웃을 코드로 작성
func makeUI() {
emailTextFieldView.backgroundColor = UIColor.darkGray
view.addSubview(emailTextFieldView) //실제 뷰에 표시하는 것
emailTextFieldView.translatesAutoresizingMaskIntoConstraints = false //이것까지 무조건 설정을 해줘야 화면에 보임.. 코드로 뷰 작성시 프레임기준으로 자동으로 올라감,그 자동을 끄는 기능
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: 200).isActive = true
emailTextFieldView.heightAnchor.constraint(equalToConstant: 40).isActive = true //높이는 기준이없기때문에 equalToConstant 선택
}
UIView 뷰를 둥글게 하는법
emailTextFieldView.layer.cornerRadius = 8
emailTextFieldView.layer.masksToBounds = true // optional
보더
button.layer.borderWidth = 1
button.layer.borderColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
alert 창 만드는방법
let alert = UIAlertController(title: "비밀번호 바꾸기", message: "비밀번호를 바꾸시겠습니까?", preferredStyle: .alert)
let success = UIAlertAction(title: "확인", style: .default) { action in
print("확인버튼이 눌렸습니다.")
}
let cancel = UIAlertAction(title: "취소", style: .cancel) { cancel in
print("취소버튼이 눌렸습니다.")
}
alert.addAction(success) //alert창을 만들고 그위에 액션을 올림
alert.addAction(cancel)
present(alert, animated: true, completion: nil) //다음화면으로 넘어가는 메서드
네비게이션 컨트롤러(코드로 작성시) 꼭 Scene Delegate에 작성해야함
//네이게이션 컨트롤러 관련
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let naviVC = UINavigationController(rootViewController: ViewController())
window?.rootViewController = naviVC
window?.makeKeyAndVisible()