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

How to set the text Value to the CBPinEnteryView? #19

Open
hassan8055 opened this issue Jul 18, 2019 · 5 comments
Open

How to set the text Value to the CBPinEnteryView? #19

hassan8055 opened this issue Jul 18, 2019 · 5 comments

Comments

@hassan8055
Copy link

I'm using this library for CNIC . Now i have to set the text to your Pin view (CNIC value coming from server). I'm looking to set the value just like the textfield.text but haven't found anything. if there is anything to set the text to Pin view . kindly let me know.

@vikasAccion
Copy link

Is this possible now to prefill PIN?

@Fawxy
Copy link
Owner

Fawxy commented Sep 6, 2019

There isn't currently but I can definitely see the use in this. I'll have a think about how to implement this best - with the codebase as it is currently I think it could end up quite messy!

Or if anyone wants to open a PR...

@vikasAccion
Copy link

There isn't currently but I can definitely see the use in this. I'll have a think about how to implement this best - with the codebase as it is currently I think it could end up quite messy!

Thanks

@musa-almatri
Copy link

musa-almatri commented Sep 6, 2019

@vikasAccion @hassan8055 Here is a workaround extension to set the pin, till a proper fix is done on the repo.

extension CBPinEntryView {
    
    open func setPin(_ pin: String) {
        self.clearEntry()
        /// String(pin.prefix(length)) to remove any extra characters
        String(pin.prefix(length)).forEach {
            _ = self.textField(self.textField, shouldChangeCharactersIn: NSRange(location: 0, length: 0), replacementString: String($0))
            self.textField.insertText(String($0))
        }
        if pin.count >= length {
            self.resignFirstResponder()
        }
    }

}

Usage

 pinEntryView.setPin(text: "123456")

@vikasAccion
Copy link

@vikasAccion @hassan8055 Here is a workaround extension to set the pin, till a proper fix is done on the repo.

extension CBPinEntryView {
    func setPin(text: String) {
        if let innerTextField = (self.subviews.compactMap { $0 as? UITextField }).first {
            self.clearEntry()
            text.forEach {
                _ = self.textField(innerTextField, shouldChangeCharactersIn: NSRange(location: 0, length: 0), replacementString: String($0))
                innerTextField.insertText(String($0))
            }
        }
    }
}

Usage

 pinEntryView.setPin(text: "123456")

Thanks Musa. Thanks a lot.
It saved my day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants