Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting while editing phone number #16

Open
k06a opened this issue Aug 28, 2014 · 1 comment
Open

Formatting while editing phone number #16

k06a opened this issue Aug 28, 2014 · 1 comment

Comments

@k06a
Copy link

k06a commented Aug 28, 2014

I am trying to achieve UITextField with formatted phone number while user input progress.
My algo:

  1. Get only digits from phone field
  2. Format digits as phone number
  3. Set UITextFields text property new formatted phone number

All works fine, until user tries to edit phone number in the middle.
I can not properly save and restore selectedRange because of formatting chars " -()"

Is there any solution for this problem?

@k06a
Copy link
Author

k06a commented Aug 28, 2014

Solved via saving number of digits to the right from cursor position:

func countDigitsRighterThatCursor(textField: UITextField) -> Int {
    let cursorPos = textField.selectedTextRange.start
    var digits = -1
    var index = 0
    for ch in textField.text.unicodeScalars {
        if digits == -1 && cursorPos == textField.positionFromPosition(textField.beginningOfDocument, offset: index) {
            digits = 0
        }
        if digits >= 0 && NSCharacterSet.decimalDigitCharacterSet().longCharacterIsMember(ch.value) {
            digits++
        }
        index++;
    }

    return digits
}

func setCursor(textField: UITextField, whereHaveDigitsFromRight digits: Int) {
    let length = countElements(textField.text)
    var index = 0
    var digitsSkipped = 0
    for ch in reverse(textField.text.unicodeScalars) {
        if digitsSkipped == digits {
            let pos = textField.positionFromPosition(textField.endOfDocument, inDirection: .Left, offset: index)
            textField.selectedTextRange = textField.textRangeFromPosition(pos, toPosition: pos)
            return
        }
        if NSCharacterSet.decimalDigitCharacterSet().longCharacterIsMember(ch.value) {
            digitsSkipped++
        }
        index++
    }
}

@IBAction func phoneChanged(textField: UITextField) {
    if countElements(textField.text) == 0 {
        return
    }

    let rightCountDigits = countDigitsRighterThatCursor(textField)
    textField.text = formattedPhoneNumber(textField.text)
    setCursor(textField, whereHaveDigitsFromRight: rightCountDigits)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant