Skip to content

Completed Project #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions PasswordTextField/PasswordTextField/PasswordField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@

import UIKit

enum PasswordStrength: String {
case weak = "WEAK"
case medium = "MEDIUM"
case strong = "STRONG"
}

class PasswordField: UIControl {



// Public API - these properties are used to fetch the final password and strength values
private (set) var password: String = ""
private (set) var passwordShow: Bool = false
private (set) var passwordStrength: PasswordStrength = .weak


private let standardMargin: CGFloat = 8.0
private let textFieldContainerHeight: CGFloat = 50.0
Expand All @@ -38,17 +49,145 @@ class PasswordField: UIControl {
private var strongView: UIView = UIView()
private var strengthDescriptionLabel: UILabel = UILabel()

private func updateButtonImage() {
let showPasswordImage = UIImage(named: "eyes-open.png")
let hidePasswordImage = UIImage(named: "eyes-closed.png")
if passwordShow {
showHideButton.setImage(showPasswordImage, for: .normal)
textField.isSecureTextEntry = false
} else {
showHideButton.setImage(hidePasswordImage, for: .normal)
textField.isSecureTextEntry = true
}
}


func setup() {
// Lay out your subviews here
self.backgroundColor = bgColor
// titleLabel Setup

titleLabel.text = "Enter Password"
titleLabel.textColor = labelTextColor
// Constraints
addSubview(titleLabel)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: standardMargin),
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: standardMargin)
])
// textField Setup
textField.translatesAutoresizingMaskIntoConstraints = false
textField.isUserInteractionEnabled = true
textField.isSecureTextEntry = true
textField.placeholder = " Password"
textField.layer.borderColor = textFieldBorderColor.cgColor
textField.layer.cornerRadius = 12
textField.layer.borderWidth = 1.0
textField.delegate = self
addSubview(textField)
// textField Constraints
NSLayoutConstraint.activate([
textField.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10),
textField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: standardMargin),
textField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -standardMargin),
textField.heightAnchor.constraint(equalToConstant: textFieldContainerHeight)
])
// textField Button
textField.rightView = showHideButton
textField.rightViewMode = .always
showHideButton.setImage(UIImage(named: "eyes-closed.png"), for: .normal)
showHideButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0)
showHideButton.addTarget(self, action: #selector(changeShowHideButton), for: .touchUpInside)

// Password Strength Views
weakView.translatesAutoresizingMaskIntoConstraints = false
weakView.layer.cornerRadius = colorViewSize.height / 2
weakView.backgroundColor = weakColor

mediumView.translatesAutoresizingMaskIntoConstraints = false
mediumView.layer.cornerRadius = colorViewSize.height / 2
mediumView.backgroundColor = unusedColor

strongView.translatesAutoresizingMaskIntoConstraints = false
strongView.layer.cornerRadius = colorViewSize.height / 2
strongView.backgroundColor = unusedColor

strengthDescriptionLabel.translatesAutoresizingMaskIntoConstraints = false
strengthDescriptionLabel.text = "Too weak"
strengthDescriptionLabel.textColor = labelTextColor
strengthDescriptionLabel.font = labelFont

addSubview(weakView)
NSLayoutConstraint.activate([
weakView.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: standardMargin),
weakView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: standardMargin),
weakView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -standardMargin),
weakView.heightAnchor.constraint(equalToConstant: colorViewSize.height),
weakView.widthAnchor.constraint(equalToConstant: colorViewSize.width)
])
addSubview(mediumView)
NSLayoutConstraint.activate([
mediumView.topAnchor.constraint(equalTo: weakView.topAnchor),
mediumView.leadingAnchor.constraint(equalTo: weakView.trailingAnchor, constant: 2),
mediumView.bottomAnchor.constraint(equalTo: weakView.bottomAnchor),
mediumView.heightAnchor.constraint(equalToConstant: colorViewSize.height),
mediumView.widthAnchor.constraint(equalToConstant: colorViewSize.width)
])
addSubview(strongView)
NSLayoutConstraint.activate([
strongView.topAnchor.constraint(equalTo: weakView.topAnchor),
strongView.leadingAnchor.constraint(equalTo: mediumView.trailingAnchor, constant: 2),
strongView.bottomAnchor.constraint(equalTo: weakView.bottomAnchor),
strongView.heightAnchor.constraint(equalToConstant: colorViewSize.height),
strongView.widthAnchor.constraint(equalToConstant: colorViewSize.width)
])
addSubview(strengthDescriptionLabel)
NSLayoutConstraint.activate([
strengthDescriptionLabel.centerYAnchor.constraint(equalTo: strongView.centerYAnchor),
strengthDescriptionLabel.leadingAnchor.constraint(equalTo: strongView.trailingAnchor, constant: 5),
strengthDescriptionLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -standardMargin)
])
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
@objc func changeShowHideButton() {
passwordShow.toggle()
updateButtonImage()
}
private func updatePasswordsStrength(strength: PasswordStrength) {
switch strength {
case .weak:
weakView.backgroundColor = weakColor
mediumView.backgroundColor = unusedColor
strongView.backgroundColor = unusedColor
strengthDescriptionLabel.text = "Too Weak"
case .medium:
weakView.backgroundColor = weakColor
mediumView.backgroundColor = mediumColor
strongView.backgroundColor = unusedColor
strengthDescriptionLabel.text = "Could be stronger"
case .strong:
weakView.backgroundColor = weakColor
mediumView.backgroundColor = mediumColor
strongView.backgroundColor = strongColor
strengthDescriptionLabel.text = "Strong password"
}
}

private func passwordStrength(password: String) {
switch password.count {
case 0...9:
updatePasswordsStrength(strength: .weak)
case 10...19:
updatePasswordsStrength(strength: .medium)
default:
updatePasswordsStrength(strength: .strong)
}
}
}

extension PasswordField: UITextFieldDelegate {
Expand All @@ -57,6 +196,13 @@ extension PasswordField: UITextFieldDelegate {
let stringRange = Range(range, in: oldText)!
let newText = oldText.replacingCharacters(in: stringRange, with: string)
// TODO: send new text to the determine strength method
passwordStrength(password: newText)
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
endEditing(true)
guard let password = textField.text else { return false }
print(password)
return false
}
}