'Strange behavior with cursor position while deleting with libPhoneNumber-iOS

I using library libPhoneNumber-iOS for formatting phone numbers in my app.

When I deleting text from the end of the text field, all works fine, but if the cursor is in the middle of a text field when I delete "-" char, cursor jumping to the end of a text field.

enter image description here

How I can fix this?

Thanks.

UPD

Chunk of code:

- (void)inputText:(NSString *)text
{
    NSString *numberWithCode = [self stringWithPhoneCodeFromString:text];
    self.text = PVNumberFormattingEnabled? [self.formatter inputString:numberWithCode] : numberWithCode;
}

- (NSString *)stringWithPhoneCodeFromString:(NSString *)string
{
    if ( [string isEqualToString:_countryModel.phoneCode] || DEVICE_IS_IPAD )
    {
        return string;
    }

    return [NSString stringWithFormat:@"%@%@", _countryModel.phoneCode? : @"", string? : @""];
}

- (void)phoneNumberTextDidChange
{
    NSString *normalizedText = [self deletePhoneCodeFromText:self.text];

    if ( phoneNumberUtil && normalizedText != nil)
    {
        normalizedText = [phoneNumberUtil normalize:normalizedText];
    }

    [self inputText:normalizedText];
}

But strange behaviour appears when someone trying to edit selectedTextRange at text field. When I added implement setSelectedTextRange:, I can't catch any calls, but whatever, cursor jumping to the end.



Solution 1:[1]

Sorry, I don't do C programming so I can't provide code examples, but can explain why the issue is happening and a possible solution as I've dealt with this same issue in Javascript.

This is due to the entire field's contents changing whenever one character is changed, which triggers a re-render and places the cursor at the end of the text string.

As far as solutions, you'd need to manually track and place the cursor yourself, and force it to remain where it should be.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ChumiestBucket