Skip to content
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
72 changes: 56 additions & 16 deletions CBPinEntryView/Classes/CBPinEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public protocol CBPinEntryViewDelegate: class {
}

private var stackView: UIStackView?
private var textField: UITextField!
private var textField: CBEntryViewTextField!

open var errorMode: Bool = false

Expand Down Expand Up @@ -188,14 +188,23 @@ public protocol CBPinEntryViewDelegate: class {
}

private func setupTextField() {
textField = UITextField(frame: bounds)
textField = CBEntryViewTextField(frame: bounds)
textField.delegate = self
textField.keyboardType = UIKeyboardType(rawValue: keyboardType) ?? .numberPad
textField.addTarget(self, action: #selector(textfieldChanged(_:)), for: .editingChanged)

textField.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(textField)

textField.isHidden = true
textField!.leadingAnchor.constraint(equalTo: stackView!.leadingAnchor, constant: 0).isActive = true
textField!.trailingAnchor.constraint(equalTo: stackView!.trailingAnchor, constant: 0).isActive = true
textField!.topAnchor.constraint(equalTo: stackView!.topAnchor, constant: 0).isActive = true
textField!.bottomAnchor.constraint(equalTo: stackView!.bottomAnchor, constant: 0).isActive = true

//textField.isHidden = true
textField.backgroundColor = .clear
textField.textColor = .clear
textField.tintColor = .clear
}

private func createButtons() {
Expand Down Expand Up @@ -329,7 +338,7 @@ extension CBPinEntryView: UITextFieldDelegate {
delegate?.entryCompleted(with: textField.text)
}
}

public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
errorMode = false
for button in entryButtons {
Expand Down Expand Up @@ -360,22 +369,43 @@ extension CBPinEntryView: UITextFieldDelegate {
let newLength = oldLength - rangeLength + replacementLength

if !deleting {
for button in entryButtons {
if button.tag == newLength {
button.layer.borderColor = entryDefaultBorderColour.cgColor
UIView.setAnimationsEnabled(false)
if !isSecure {
button.setTitle(string, for: .normal)
if replacementLength == 1 {
for button in entryButtons {
if button.tag == newLength {
UIView.setAnimationsEnabled(false)
button.layer.borderColor = entryDefaultBorderColour.cgColor
if !isSecure {
button.setTitle(string, for: .normal)
} else {
button.setTitle(secureCharacter, for: .normal)
}
UIView.setAnimationsEnabled(true)
} else if button.tag == newLength + 1 {
button.layer.borderColor = entryBorderColour.cgColor
button.backgroundColor = entryEditingBackgroundColour
} else {
button.setTitle(secureCharacter, for: .normal)
button.layer.borderColor = entryDefaultBorderColour.cgColor
button.backgroundColor = entryBackgroundColour
}
UIView.setAnimationsEnabled(true)
} else if button.tag == newLength + 1 {
}
} else {
UIView.setAnimationsEnabled(false)
for i in 0..<replacementLength {
let buttonIndex = oldLength + i + 1
entryButtons.filter({ $0.tag == buttonIndex }).forEach { (button: UIButton) in
button.layer.borderColor = entryDefaultBorderColour.cgColor
if !isSecure {
button.setTitle(string[i], for: .normal)
} else {
button.setTitle(secureCharacter, for: .normal)
}
}
}
UIView.setAnimationsEnabled(true)
let buttonIndex = newLength + 1
entryButtons.filter({ $0.tag == buttonIndex }).forEach { (button: UIButton) in
button.layer.borderColor = entryBorderColour.cgColor
button.backgroundColor = entryEditingBackgroundColour
} else {
button.layer.borderColor = entryDefaultBorderColour.cgColor
button.backgroundColor = entryBackgroundColour
}
}
} else {
Expand Down Expand Up @@ -415,3 +445,13 @@ extension UIButton {
line.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
}

class CBEntryViewTextField: UITextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return (action == #selector(UIResponderStandardEditActions.paste(_:)))
}

override func canPaste(_ itemProviders: [NSItemProvider]) -> Bool {
return true
}
}