CBPinEntryView icon indicating copy to clipboard operation
CBPinEntryView copied to clipboard

How to set the text Value to the CBPinEnteryView?

Open hassan8055 opened this issue 5 years ago • 5 comments

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.

hassan8055 avatar Jul 18 '19 05:07 hassan8055

Is this possible now to prefill PIN?

vikasAccion avatar Sep 06 '19 12:09 vikasAccion

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...

Fawxy avatar Sep 06 '19 12:09 Fawxy

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

vikasAccion avatar Sep 06 '19 12:09 vikasAccion

@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")

musa-almatri avatar Sep 06 '19 23:09 musa-almatri

@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.

vikasAccion avatar Sep 07 '19 12:09 vikasAccion