@@ -14,23 +14,15 @@ class ViewController: UIViewController, FormattedTextFieldDelegate {
14
14
15
15
override func viewDidLoad( ) {
16
16
super. viewDidLoad ( )
17
- textField. textFormatter = MaskTextFormatter ( mask: " +× ××× ××× ×××××××× " )
18
- // Do any additional setup after loading the view, typically from a nib.
19
- }
20
-
21
- override func didReceiveMemoryWarning( ) {
22
- super. didReceiveMemoryWarning ( )
23
- // Dispose of any resources that can be recreated.
17
+ textField. textAlignment = . center
18
+ textField. placeholderMode = . always
19
+ updateTextFieldMask ( )
24
20
}
25
21
26
22
// MARK: - Actions
27
23
28
24
@IBAction private func textFieldTextChanged( _ textField: FormattedTextField ) {
29
- let textMask = mask ( forPhoneNumber: textField. unformattedText ?? " " ) ?? " +× ××× ××× ×××××××× "
30
- let formatter = textField. textFormatter! as! MaskTextFormatter
31
- if formatter. mask != textMask {
32
- textField. textFormatter = MaskTextFormatter ( mask: textMask)
33
- }
25
+ updateTextFieldMask ( )
34
26
}
35
27
36
28
// MARK: - FormattedTextFieldDelegate
@@ -39,16 +31,28 @@ class ViewController: UIViewController, FormattedTextFieldDelegate {
39
31
return ( replacementString. isEmpty || Int ( replacementString) != nil )
40
32
}
41
33
42
- // MARK: - Actions
34
+ // MARK: - Private
35
+
36
+ private func updateTextFieldMask( ) {
37
+ let textMask = mask ( forPhoneNumber: textField. unformattedText ?? " " ) ?? " +_ ___ ___ ________ "
38
+ let formatter = textField. textFormatter as? MaskTextFormatter
39
+ if formatter? . mask != textMask {
40
+ textField. textFormatter = MaskTextFormatter ( mask: textMask, maskSymbol: " _ " )
41
+ }
42
+
43
+ let placeholderStartIndex = textMask. index ( textMask. startIndex, offsetBy: ( textField. text? . count ?? 0 ) )
44
+ textField. placeholder = String ( textMask [ placeholderStartIndex... ] )
45
+ }
46
+
43
47
private func mask( forPhoneNumber phoneNumber: String ) -> String ? {
44
48
let masks : [ ( format: String , mask: String ) ] = [
45
- ( " 1 " , " +× (×××) ××× ×××× " ) ,
46
- ( " 7 " , " +× (×××) ××× ×××× " ) ,
47
- ( " 44 " , " +×× (×××) ×××× ×××× " ) ,
48
- ( " 49 " , " +×× (×××××) ×××-×××× " ) ,
49
- ( " 54 " , " +×× (×××) ××× ×××× " ) ,
50
- ( " 86 " , " +×× (×××) ××× ×××× " ) ,
51
- ( " 358 " , " +××× × ××× ××× " ) ,
49
+ ( " 1 " , " +_ (___) ___ ____ " ) ,
50
+ ( " 7 " , " +_ (___) ___ ____ " ) ,
51
+ ( " 44 " , " +__ (___) ____ ____ " ) ,
52
+ ( " 49 " , " +__ (_____) ___-____ " ) ,
53
+ ( " 54 " , " +__ (___) ___ ____ " ) ,
54
+ ( " 86 " , " +__ (___) ___ ____ " ) ,
55
+ ( " 358 " , " +___ _ ___ ___ " ) ,
52
56
]
53
57
return masks. first { ( mask, _) -> Bool in
54
58
phoneNumber. hasPrefix ( mask)
0 commit comments