Skip to content

Added a secure text input option #7

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 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions Demo/CustomItemView/CircleItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import UIKit
import PinCodeInputView

final class CircleItemView: UIView, ItemType {
var isSecureText: Bool = false


var text: Character? = nil {
didSet {
Expand Down
3 changes: 3 additions & 0 deletions Demo/CustomItemView/PasswordItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import UIKit
import PinCodeInputView

final class PasswordItemView: UIView, ItemType {

var isSecureText: Bool = false


var text: Character? = nil {
didSet {
Expand Down
2 changes: 2 additions & 0 deletions Demo/CustomItemView/UnderlineItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import UIKit
import PinCodeInputView

final class UnderlineItemView: UIView, ItemType {
var isSecureText: Bool = false


var text: Character? = nil {
didSet {
Expand Down
3 changes: 2 additions & 1 deletion Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class ViewController: UIViewController {
itemFactory: {
return ItemView()
},
autoresizes: true)
autoresizes: true,
isSecureText: true)

// customize item view (underline)
// let pinCodeInputView: PinCodeInputView<UnderlineItemView> = .init(
Expand Down
1 change: 1 addition & 0 deletions PinCodeInputView/ItemType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public protocol ItemType {

var text: Character? { get set }
var isHiddenCursor: Bool { get set }
var isSecureText: Bool {get set}
func set(appearance: ItemAppearance)
}
10 changes: 9 additions & 1 deletion PinCodeInputView/ItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ItemView: UIView, ItemType {
label.text = nil
return
}
label.text = String(text)
label.text = isSecureText ? "*" : String(text)
}
}

Expand All @@ -33,6 +33,14 @@ public class ItemView: UIView, ItemType {
}
}
}

public var isSecureText: Bool = false {
didSet {
if let text = text {
label.text = isSecureText ? "*" : String(text)
}
}
}

public let label: UILabel = .init()
public let cursor: UIView = .init()
Expand Down
11 changes: 10 additions & 1 deletion PinCodeInputView/PinCodeInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class PinCodeInputView<T: UIView & ItemType>: UIControl, UITextInputTrait
override public var intrinsicContentSize: CGSize {
return stackView.bounds.size
}

public var isSecureText: Bool = false {
didSet {
self.items.forEach({$0.itemView.isSecureText = self.isSecureText})
}
}

private let digit: Int
private let itemSpacing: CGFloat
Expand All @@ -63,7 +69,8 @@ public class PinCodeInputView<T: UIView & ItemType>: UIControl, UITextInputTrait
digit: Int,
itemSpacing: CGFloat,
itemFactory: @escaping (() -> T),
autoresizes: Bool = false) {
autoresizes: Bool = false,
isSecureText: Bool = false) {

self.digit = digit
self.itemSpacing = itemSpacing
Expand All @@ -79,10 +86,12 @@ public class PinCodeInputView<T: UIView & ItemType>: UIControl, UITextInputTrait
}
return item
}
self.isSecureText = isSecureText
self.stackView.frame = self.bounds
addSubview(stackView)

items.forEach { stackView.addArrangedSubview($0) }
items.forEach({$0.itemView.isSecureText = self.isSecureText})
stackView.spacing = itemSpacing
stackView.axis = .horizontal
stackView.distribution = .fillEqually
Expand Down