slight refactoring to fit code style

slightly refactored to fit code style of project
pull/1/head
Hannes Hofer 10 years ago
parent bde62ed7cb
commit 9703e601b0

@ -382,42 +382,44 @@
#pragma mark - UITextFieldDelegate #pragma mark - UITextFieldDelegate
- (NSUInteger) calculateLocationOffset:(NSUInteger)location { - (NSUInteger) calculateLocationOffset:(NSUInteger)location {
uint offset = 0, phonenumberposition = 0; NSUInteger offset = 0, phonenumberposition = 0;
for (uint i=0;i<location;i++) { for (NSUInteger i = 0; i < location; i++) {
if ([_phoneNumberTextField.text characterAtIndex:i] != [_enteredPhoneNumber characterAtIndex:phonenumberposition]) if ([_phoneNumberTextField.text characterAtIndex:i] != [_enteredPhoneNumber characterAtIndex:phonenumberposition]) {
offset ++; offset++;
else } else {
phonenumberposition ++; phonenumberposition++;
}
} }
return offset; return offset;
} }
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string { replacementString:(NSString *)string {
uint offset = [self calculateLocationOffset:range.location]; NSUInteger offset = [self calculateLocationOffset:range.location];
unsigned short currentPosCharacter = 0; unichar currentPoschar = 0;
range.location -= offset; range.location -= offset;
BOOL handleBackspace = range.length == 1; BOOL handleBackspace = range.length == 1;
if (handleBackspace) { if (handleBackspace) {
if ((range.location + offset) < _phoneNumberTextField.text.length) if ((range.location + offset) < _phoneNumberTextField.text.length) {
currentPosCharacter = [_phoneNumberTextField.text characterAtIndex:(range.location + offset)]; currentPoschar = [_phoneNumberTextField.text characterAtIndex:(range.location + offset)];
}
if (currentPosCharacter < '0' || currentPosCharacter > '9') if ((currentPoschar < '0' || currentPoschar > '9') && currentPoschar != 0) {
range.location --; range.location--;
}
NSRange backspaceRange = NSMakeRange(range.location, 1); NSRange backspaceRange = NSMakeRange(range.location, 1);
[_enteredPhoneNumber replaceCharactersInRange:backspaceRange withString:string]; [_enteredPhoneNumber replaceCharactersInRange:backspaceRange withString:string];
range.location ++; range.location++;
} else { } else {
NSString* sanitizedString = [[string componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet ] invertedSet]] componentsJoinedByString:@""]; NSString* sanitizedString = [[string componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet ] invertedSet]] componentsJoinedByString:@""];
NSMutableString* mutablePhoneNumber = [NSMutableString stringWithString:_enteredPhoneNumber]; NSMutableString* mutablePhoneNumber = [NSMutableString stringWithString:_enteredPhoneNumber];
[mutablePhoneNumber insertString:sanitizedString atIndex:range.location]; [mutablePhoneNumber insertString:sanitizedString atIndex:range.location];
_enteredPhoneNumber = mutablePhoneNumber; _enteredPhoneNumber = mutablePhoneNumber;
if ((range.location + offset +1) < _phoneNumberTextField.text.length) if ((range.location + offset + 1) < _phoneNumberTextField.text.length) {
currentPosCharacter = [_phoneNumberTextField.text characterAtIndex:(range.location + offset +1)]; currentPoschar = [_phoneNumberTextField.text characterAtIndex:(range.location + offset + 1)];
}
if ((currentPosCharacter < '0' || currentPosCharacter > '9') && currentPosCharacter != 0) if ((currentPoschar < '0' || currentPoschar > '9') && currentPoschar != 0) {
range.location++; range.location++;
}
} }
[self updatePhoneNumberFieldWithString:_enteredPhoneNumber cursorposition:range.location+offset]; [self updatePhoneNumberFieldWithString:_enteredPhoneNumber cursorposition:range.location+offset];
@ -431,8 +433,9 @@
cursorpos += result.length - _phoneNumberTextField.text.length; cursorpos += result.length - _phoneNumberTextField.text.length;
_phoneNumberTextField.text = result; _phoneNumberTextField.text = result;
UITextPosition *position = [_phoneNumberTextField positionFromPosition:[_phoneNumberTextField beginningOfDocument] UITextPosition *position = [_phoneNumberTextField positionFromPosition:[_phoneNumberTextField beginningOfDocument]
offset:(NSInteger)cursorpos]; offset:(NSInteger)cursorpos];
[_phoneNumberTextField setSelectedTextRange:[_phoneNumberTextField textRangeFromPosition:position toPosition:position]]; [_phoneNumberTextField setSelectedTextRange:[_phoneNumberTextField textRangeFromPosition:position
toPosition:position]];
} }
@end @end

Loading…
Cancel
Save