Skip to content

Commit 5394338

Browse files
committed
Update example
1 parent b68ae80 commit 5394338

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

Example/Example.gif

-6.36 KB
Loading

Example/Example/Base.lproj/Main.storyboard

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="16A323" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina6_1" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
36
<dependencies>
47
<deployment identifier="iOS"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11161"/>
6-
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
79
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
810
</dependencies>
911
<scenes>
@@ -16,11 +18,11 @@
1618
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1719
</layoutGuides>
1820
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
19-
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
21+
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
2022
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2123
<subviews>
22-
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="_ (___) ____ ____" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="vrS-aj-jzx" customClass="FormattedTextField" customModule="Example" customModuleProvider="target">
23-
<rect key="frame" x="16" y="40" width="343" height="30"/>
24+
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="leading" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="_ (___) ____ ____" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="vrS-aj-jzx" customClass="FormattedTextField" customModule="Example" customModuleProvider="target">
25+
<rect key="frame" x="20" y="64" width="374" height="30"/>
2426
<nil key="textColor"/>
2527
<fontDescription key="fontDescription" type="system" pointSize="20"/>
2628
<textInputTraits key="textInputTraits" smartInsertDeleteType="no"/>

Example/Example/ViewController.swift

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,15 @@ class ViewController: UIViewController, FormattedTextFieldDelegate {
1414

1515
override func viewDidLoad() {
1616
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()
2420
}
2521

2622
// MARK: - Actions
2723

2824
@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()
3426
}
3527

3628
// MARK: - FormattedTextFieldDelegate
@@ -39,16 +31,28 @@ class ViewController: UIViewController, FormattedTextFieldDelegate {
3931
return (replacementString.isEmpty || Int(replacementString) != nil)
4032
}
4133

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+
4347
private func mask(forPhoneNumber phoneNumber: String) -> String? {
4448
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", "+___ _ ___ ___"),
5256
]
5357
return masks.first { (mask, _) -> Bool in
5458
phoneNumber.hasPrefix(mask)

0 commit comments

Comments
 (0)