1

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.

2
  • Can you provide the code that applies formatting behaviour to the textfield?
    – mas'an
    Feb 2, 2018 at 9:53
  • @mas'an I added code and some info Feb 2, 2018 at 12:45

1 Answer 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.