Guest User

Untitled

a guest
Jan 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. self.mainTextField.delegate = self
  2.  
  3. self.mainTextField(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
  4.  
  5. func textFieldDidChange(textField: UITextField) {
  6. if textField == self.mainTextField {
  7.  
  8. // Some locales use different punctuations.
  9. var textFormatted = textField.text?.replacingOccurrences(of: ",", with: "")
  10. textFormatted = textFormatted?.replacingOccurrences(of: ".", with: "")
  11.  
  12. let numberFormatter = NumberFormatter()
  13. numberFormatter.numberStyle = .decimal
  14. if let text = textFormatted, let textAsInt = Int(text) {
  15. textField.text = numberFormatter.string(from: NSNumber(value: textAsInt))
  16. }
  17. }
  18. }
Add Comment
Please, Sign In to add comment