I was wondering how I can format the textField that I'm using for a phone number (ie like the "Add New Contact" page on the iPhone. When I enter in a new mobile phone, ex. 1236890987 it formats it as (123) 689-0987.) I already have the keyboard set as the number pad.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- back button text does not change
相关文章
- 现在使用swift开发ios应用好还是swift?
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- How do you detect key up / key down events from a
Swift 4 (and without NSString)
for format +X(XXX)XXX-XXXX or +X(XXX)XXX-XX-XX Updated and slightly
I use this format X (XXX) XXX XX XX it is work in Turkey,
I use it with TableView with Swift 4
and you can call this func in
in any indexPath that your text field in it
for example my textfield in indexPath number 1 so the code will be
Hopefully, what i'm going to say, will be helpfull for new people programming on iOS, as I am. I did what zingle-dingle suggest (thanks a lot!). To help new ones the code plus what i'll list could help you. 1. you have to add the UITextFieldDelegate on the header file. 2. The UITextField should bind the delegate with the view, in my case the UIViewController, which is the header file. 3. the UITextField should be instatiated, it means, yourtextfile.delegate = self, on the ".m" file.
Here is my Swift 2 code from slightly localised from a UK perspective.
It will format:
+11234567890 as +1 (123) 456 7890
+33123456789 as +33 1 23 45 67 89
+441234123456 as +44 1234 123456 (this has been further localised as 01234 123456) because I don't need to see the country code for UK numbers.
Call as follows:
If you have any other country codes and formats or improvements to the code please let me know.
Enjoy.
I have a solution for this but it is having some drawback, see if you can modify and use it. By using this you can achieve both restrict phone number to 10 digit and format it as per US format.
Implement it in UITextField Delegate method
Here is my take of it. Which is close to what Apple does in the Phone and Contacts app (at least when your region is set to U.S., I am not sure if the behavior changes per region).
I was specifically interested in formatting up to
1 (123) 123-1234
and supporting longer numbers with no formatting. There is also a bug in just checkingrange.length == 1
(for delete/backspace) in the other solutions which prevents a user from selecting the entire string or part of it and pressing the delete/backspace key, this addresses that situation.There are some strange behaviors that occur when you start selecting a range in the middle and edit, where the cursor always ends up at the end of the string due to setting the text fields value. I am not sure how to reposition the cursor in a
UITextField
, I presume Apple is actually using aUITextView
in the Contacts and Phone apps as they maintain cursor position whilst doing this inline formatting, they seem to handle all the little nuances! I wish they'd just give us this out of the box.