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

Wrong masking when setting text programmatically #20

Open
juanpdelat opened this issue Nov 9, 2016 · 3 comments
Open

Wrong masking when setting text programmatically #20

juanpdelat opened this issue Nov 9, 2016 · 3 comments

Comments

@juanpdelat
Copy link

Trying to set a phone number programmatically before the user starts typing manually but the results are always wrong. When using textfield.text = ... directly, no mask is applied. When using setTextWithMask: the following happens:

Mask = "(###) ###-####"
Text needed: 1234567890, also tried (123) 456-7890
Result: (234) 789-0
Expected: (123) 456-7890

I found a solution in the mean time by setting the text one character at a time:

phoneText.characters.forEach({
    let text = phoneTextField.text ?? ""
    phoneTextField.setTextWithMask( text + String($0) )
})
@SeRG1k17
Copy link

SeRG1k17 commented Jan 4, 2017

Objective-C:

                for (int i=0; i<phoneText.length; i++) {
                unichar ch = [phoneText characterAtIndex:i];
                
                NSMutableString *combinedString;
                if (phoneCell.phoneTextField.text) {
                    combinedString=[NSMutableString stringWithString:phoneCell.phoneTextField.text];
                } else {
                    combinedString=[NSMutableString stringWithString:@""];
                }
                
                NSString *chString = [NSString stringWithFormat:@"%c",ch];
                [combinedString appendString:chString];
                [phoneCell.phoneTextField setTextWithMask: combinedString];
            }

@halakaluf
Copy link

I sended a PR with this fix, now we wait.

@CavalcanteLeo
Copy link

CavalcanteLeo commented Dec 21, 2018

@halakaluf it crashes =/

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

No branches or pull requests

4 participants