0

I am using swift language. I have two UITextFields where the user enters data. It's a serial number field. If the user enters 123456789, I want it displayed as 123-45-6789 and 12-3456789 as the user types. How is this possible?

Ali Raza
  • 2,796
  • 1
  • 20
  • 24
  • 3
    see this link it helps you http://stackoverflow.com/questions/1246439/uitextfield-for-phone-number – Anbu.Karthik Sep 07 '15 at 09:41
  • This link is not completing my desire requirement. Because before my question i have seen that link. If you read your link it's different formatting and i already attempt that code which is given in this link. – Ali Raza Sep 07 '15 at 10:00

1 Answers1

-1

Here is how you could format a string

let serialNumber = 123456789
let snStr = "\(123456789)"
let nsStr = snStr as NSString

let start = nsStr.substringWithRange(NSRange(location: 0, length: 3))

let end = nsStr.substringWithRange(NSRange(location: 3, length: 6))

let result = "\(start)-\(end)"

enter image description here

What you should do is create an object that implements CustomStringConvertible or Printable depending on the version of swift you are using.

Then in your description method/stored property you would put code similar to what I have above. As the user enters text the textfield is going to store the property in the model of your MVC and eventually it will then be displayed on the view. The View - will in turn call the description part of your object and render the text to the screen.

Jeef
  • 26,861
  • 21
  • 78
  • 156