패스워드용 텍스트필드를 만들려면 Secure설정을 주면 된다.
하지만 이렇게 하면 포커스가 옴겨졌다 다시 텍스트필드를 수정하려고하면 기존 텍스트 내용이 날라간다.
보안이 필요한 텍스트필드니 당연하다 생각했는데
이걸 원치 않는 사람이 있나보다
수정되는 이전 값이 남아 있고, 값이 숨기고 보이고 할 수 있고 그런 텍스트 필드를 원하는데 어떻게 해야할까요?
라는 질문을 글을 보고 이렇게 하면 되지 않을까 하고 해봤는데
솔직히 별로 맘에 안든다.
다른 방법이 없으려나
보안이 보안답지 않고....
그렇다고 소스가 깔끔하지도 않고....
나중에 텍스트필드를 새로 클래스 만들어서 하면 좀 깔끔해질거같긴한데
미묘하다.
https://github.com/wiwi-git/Hide_And_Show_TextField
var textField: UITextField!
var showButton: UIButton!
private var hiddenText: String = "" {
didSet{
print("hiddenText: " + hiddenText)
}
}
var isShowMode = false
let secretChar = "*"
override func viewDidLoad() {
super.viewDidLoad()
textField = createTextField()
showButton = createShowButton()
view.addSubview(textField)
view.addSubview(showButton)
let viewGuide = view.safeAreaLayoutGuide
textField.translatesAutoresizingMaskIntoConstraints = false
showButton.translatesAutoresizingMaskIntoConstraints = false
[
textField.topAnchor.constraint(equalTo: viewGuide.topAnchor, constant: 30),
textField.leadingAnchor.constraint(equalTo: viewGuide.leadingAnchor, constant: 16),
textField.trailingAnchor.constraint(equalTo: viewGuide.trailingAnchor,constant: -16),
textField.heightAnchor.constraint(equalToConstant: textField.frame.height),
showButton.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: 8),
showButton.trailingAnchor.constraint(equalTo: viewGuide.trailingAnchor,constant: -16),
showButton.heightAnchor.constraint(equalToConstant: textField.frame.height),
showButton.widthAnchor.constraint(equalToConstant: 100)
].forEach { $0.isActive = true }
}
func createTextField() -> UITextField {
let textFieldSize = CGSize(width: 0, height: 35)
let textField = UITextField(frame: CGRect(origin: .zero, size: textFieldSize))
textField.layer.borderWidth = 1
textField.layer.cornerRadius = 10
textField.textContentType = .password
textField.placeholder = "password"
textField.keyboardType = .asciiCapable
textField.isSelected = false
textField.addTarget(self, action: #selector(textFieldEditingChangedAction(_:)), for: .editingChanged)
return textField
}
func createShowButton() -> UIButton {
let showButton = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 35)))
showButton.setTitle("Set Show", for: .normal)
showButton.setTitleColor(.systemBlue, for: .normal)
showButton.backgroundColor = .clear
showButton.setBackgroundImage(nil, for: .normal)
showButton.layer.borderWidth = 1
showButton.layer.borderColor = UIColor.systemBlue.cgColor
showButton.layer.cornerRadius = 10
showButton.addTarget(self, action: #selector(showButtonAction(_:)), for: .touchUpInside)
return showButton
}
@objc func showButtonAction(_ sender: UIButton) {
if ( sender.titleLabel?.text == "Set Show") {
self.isShowMode = true
sender.setTitle("Set Hide", for: .normal)
sender.layer.borderColor = UIColor.clear.cgColor
sender.backgroundColor = .darkGray
sender.setTitleColor(.white, for: .normal)
self.textField.text = hiddenText
} else {
self.isShowMode = false
sender.setTitle("Set Show", for: .normal)
sender.setTitleColor(.systemBlue, for: .normal)
sender.backgroundColor = .clear
sender.layer.borderColor = UIColor.systemBlue.cgColor
textField.text = ""
hiddenText.forEach { _ in
textField.text! += secretChar
}
}
}
@objc func textFieldEditingChangedAction(_ textField: UITextField) {
guard let last = textField.text?.last else {
hiddenText = ""
return
}
if textField.text?.count ?? 0 > hiddenText.count {
if (!isShowMode) {
textField.text?.removeLast()
textField.text! += secretChar
}
hiddenText += String(last)
} else {
hiddenText.removeLast()
}
}
반응형
'iOS > swift' 카테고리의 다른 글
collectionView의 scrollToItem이 동작하지 않는다. (0) | 2022.03.14 |
---|---|
돈 자리수 표시하기 (0) | 2022.03.08 |
CollectionView ScrollToItem Error, 이전에는 잘됐는데! (0) | 2022.02.07 |
메모. iOS 시작화면 애니메이션 - 카메라 조리개 (0) | 2022.01.24 |
메모. 스위프트 알고리즘 (0) | 2022.01.12 |