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 Manually #33

Open
LuaiKalkatawi opened this issue Dec 2, 2016 · 7 comments
Open

Formatting Manually #33

LuaiKalkatawi opened this issue Dec 2, 2016 · 7 comments

Comments

@LuaiKalkatawi
Copy link

I'm trying to format the credit card number without setting the textfield class with BKCardNumberField. So I have typed this

   let bkcardNumberFormatter = BKCardNumberFormatter()

  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    textField.text = bkcardNumberFormatter.formattedStringFromRawString(newString)
    return true
}

it is formatting but it is adding one more character in the end and can't delete text after whitespace. So please could you help me with it?

@bkook
Copy link
Owner

bkook commented Dec 2, 2016

I think you should return false in shouldChangeCharactersInRange method because you are now updating text value manually.
If you return true, your manual update executed and then iOS also updates text field again.

@LuaiKalkatawi
Copy link
Author

I have returned it false but it is not changing anything at all. Please need your help.

@bkook
Copy link
Owner

bkook commented Dec 8, 2016

By the way, what did you passed into formattedStringFromRawString?
What's newString?

@LuaiKalkatawi
Copy link
Author

LuaiKalkatawi commented Dec 8, 2016

@bkook newString was textField.text as NSString

            let newString = textField.text! as NSString
            textField.text = cardNumberFormatter.formattedStringFromRawString(textField.text)

@bkook
Copy link
Owner

bkook commented Dec 8, 2016

You have to pass new string instead of textField.text.
Please check textField:shouldChangeCharactersInRange:replacementString: method in BKCardNumberField.m

// objc code
NSString *newString = [currentText stringByReplacingCharactersInRange:range withString:string];

@LuaiKalkatawi
Copy link
Author

LuaiKalkatawi commented Dec 8, 2016

@bkook I have typed this before and returned false it is formatting well but it is not deleting the empty space.

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        let currentText = textField.text! as NSString
        
        let newString = currentText.stringByReplacingCharactersInRange(range, withString: string)
        textField.text = cardNumberFormatter.formattedStringFromRawString(newString)
        
        return false
    }

@bkook
Copy link
Owner

bkook commented Dec 8, 2016

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

2 participants