Skip to content
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

Customize secure character #58

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
3 changes: 2 additions & 1 deletion Pod/PinCodeTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import UIKit
@IBInspectable public var underlineColor: UIColor = UIColor.darkGray
@IBInspectable public var updatedUnderlineColor: UIColor = UIColor.clear
@IBInspectable public var secureText: Bool = false
@IBInspectable public var secureCharacter: String = "•"
@IBInspectable public var needToUpdateUnderlines: Bool = true
@IBInspectable public var characterBackgroundColor: UIColor = UIColor.clear
@IBInspectable public var characterBackgroundCornerRadius: CGFloat = 0
Expand Down Expand Up @@ -196,7 +197,7 @@ import UIKit
}

private func updateLabels() {
let textHelper = TextHelper(text: text, placeholder: placeholderText, isSecure: isSecureTextEntry)
let textHelper = TextHelper(text: text, placeholder: placeholderText, isSecure: isSecureTextEntry, charSubstitute: Character(secureCharacter))
for label in labels {
let index = labels.firstIndex(of: label) ?? 0
let currentCharacter = textHelper.character(atIndex: index)
Expand Down
6 changes: 4 additions & 2 deletions Pod/TextHelper/TextHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class TextHelper {
let text: String?
let placeholderText: String?
let isSecureTextEntry: Bool
let charSubstitute: Character

init(text: String?, placeholder: String?, isSecure: Bool = false) {
init(text: String?, placeholder: String?, isSecure: Bool = false, charSubstitute: Character = "•") {
self.text = text
self.placeholderText = placeholder
self.isSecureTextEntry = isSecure
self.charSubstitute = charSubstitute
}

func character(atIndex i: Int) -> Character? {
Expand All @@ -25,7 +27,7 @@ class TextHelper {
let character: Character?
if i < inputTextCount {
let string = text ?? ""
character = isSecureTextEntry ? "•" : string[string.index(string.startIndex, offsetBy: i)]
character = isSecureTextEntry ? charSubstitute : string[string.index(string.startIndex, offsetBy: i)]
}
else if i < placeholderTextLength {
let string = placeholderText ?? ""
Expand Down