VMaskTextField icon indicating copy to clipboard operation
VMaskTextField copied to clipboard

Wrong masking when setting text programmatically

Open juanpdelat opened this issue 9 years ago • 3 comments

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) )
})

juanpdelat avatar Nov 09 '16 16:11 juanpdelat

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];
            }

SeRG1k17 avatar Jan 04 '17 22:01 SeRG1k17

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

halakaluf avatar Feb 14 '18 15:02 halakaluf

@halakaluf it crashes =/

CavalcanteLeo avatar Dec 21 '18 12:12 CavalcanteLeo