I have a text field where the user enters data. It's a phone number field. If the user enters 1234567890
, I want it displayed as (123)-(456)-7890
as the user types. How is this possible?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- 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
- Swift - hide pickerView after value selected
In SWIFT 3
Swift 3 string.index.advancedBy(3) alternate:
First, add
UITextFieldDelegate
to your.h
file and Delegate yourUITextField
in thenib
file.Second, add this code to your
.m
file :Swift Formatted Phone Number
Improves oppon @datinc's answer, input such as
1123df#$@$gdfg45-+678dfg901
will be output as+11(234)567-8901
This will help you
Format (xxx) xxx-xxxx
Relevant for U.S. Phone Numbers:
Adding to @wan's post, I added a conditional statement if the user starts with the country code (1). That way, it will format to be: 1 (XXX) XXX-XXXX instead of (1XX) XXX-XXXX.