Skip to content
Open
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
23 changes: 13 additions & 10 deletions Pod/Classes/VMaskTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ - (instancetype)initWithFrame:(CGRect)frame

-(void) setTextWithMask:(NSString *) text{
NSAssert(_mask!=nil, @"Mask is nil.");
int currentStartRange = 0;
for (int i = 0; i < text.length; i++) {
currentStartRange = i;
if (self.text.length == _mask.length) {
break;
}
[self shouldChangeCharactersInRange:NSMakeRange(self.text.length, 0) replacementString:[NSString stringWithFormat:@"%c",[text characterAtIndex:i]]];

//fix bug reported on https://github.com/viniciusmo/VMaskTextField/issues/20
NSString *strToGetCharacterInMask = [_mask substringWithRange:NSMakeRange(0, i)];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crashes here

NSString *strWithCharacterInMask = [strToGetCharacterInMask stringByReplacingOccurrencesOfString:@"#" withString:@""];
int totalOfCharacterInMask = [strWithCharacterInMask length];
int totalOfRangeToAdd = [[[_mask substringWithRange:NSMakeRange(0, i+totalOfCharacterInMask)] stringByReplacingOccurrencesOfString:@"#" withString:@""] length];
currentStartRange += totalOfRangeToAdd;

[self shouldChangeCharactersInRange:NSMakeRange(currentStartRange, 0) replacementString:[NSString stringWithFormat:@"%c",[text characterAtIndex:i]]];
}
}

Expand All @@ -47,15 +57,8 @@ - (BOOL)shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString
}

-(NSString *)raw{
NSMutableString *string = [NSMutableString new];
for (int i=0; i<self.text.length; i++) {
unichar charMask = [self.mask characterAtIndex:i];
unichar charText = [self.text characterAtIndex:i];
if (charMask == '#') {
[string appendFormat:@"%c", charText];
}
}
return string;
NSCharacterSet *charsToRemove = [NSCharacterSet characterSetWithCharactersInString:self.mask];
return [[self.text componentsSeparatedByCharactersInSet: charsToRemove] componentsJoinedByString: @""];
}

-(double) rawToDouble{
Expand Down