Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- self.mainTextField.delegate = self
- self.mainTextField(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
- func textFieldDidChange(textField: UITextField) {
- if textField == self.mainTextField {
- // Some locales use different punctuations.
- var textFormatted = textField.text?.replacingOccurrences(of: ",", with: "")
- textFormatted = textFormatted?.replacingOccurrences(of: ".", with: "")
- let numberFormatter = NumberFormatter()
- numberFormatter.numberStyle = .decimal
- if let text = textFormatted, let textAsInt = Int(text) {
- textField.text = numberFormatter.string(from: NSNumber(value: textAsInt))
- }
- }
- }
Add Comment
Please, Sign In to add comment