Skip to content

Commit

Permalink
Added the prefill method.
Browse files Browse the repository at this point in the history
  • Loading branch information
NikKovIos committed Mar 8, 2019
1 parent 3f3de5d commit 011f709
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
9 changes: 8 additions & 1 deletion Example/ExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import UIKit
//import NKVPhonePicker (import if needed)

class ExampleViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
Expand Down Expand Up @@ -80,8 +81,14 @@ class ExampleViewController: UIViewController {
bottomTextField.font = UIFont.boldSystemFont(ofSize: 25)
bottomTextField.textColor = UIColor.white
bottomTextField.textFieldTextInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)

// prefilling
let source = NKVSource(countryCode: "ru")
bottomTextField.setCurrentCountryInitially = false
bottomTextField.preFillText(source: source, number: 7999432423)

bottomTextField.translatesAutoresizingMaskIntoConstraints = false
self.scrollView.addSubview(bottomTextField)
scrollView.addSubview(bottomTextField)

let views: [String : Any] = ["bottomTextField": self.bottomTextField]
let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat:
Expand Down
6 changes: 3 additions & 3 deletions Sources/Bundle/Countries.bundle/Data/countryCodes.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
{
"name": "DummyPlaceholder",
"dial_code": "?",
"code": "DP",
"name": "emptyCountry",
"dial_code": "",
"code": "EMPTY",
"format": "################"
},
{
Expand Down
5 changes: 3 additions & 2 deletions Sources/Main/NKVPhonePickerPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import UIKit
class NKVPhonePickerPresenter {

/// Country setter
public func setCountry(source: NKVSource) {
public func setCountry(source: NKVSource?) {
let source = source ?? NKVSource(countryCode: "EMPTY")
setFlag(source: source)
setCode(source: source)
}
Expand All @@ -28,7 +29,7 @@ class NKVPhonePickerPresenter {
pe = PhoneExtension(source: source)?.phoneExtension ?? "?"
case .phoneExtension(let phoneExtension):
pe = phoneExtension.phoneExtension
}
}

textField.text = ""
textField.text = "\(pe)"
Expand Down
26 changes: 24 additions & 2 deletions Sources/Main/NKVPhonePickerTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ open class NKVPhonePickerTextField: TextFieldPatternFormat {
/// Set to 'false' if you don't need to scroll to selected country in when CountryPickerViewController did appear.
public var shouldScrollToSelectedCountry: Bool = true

/// If true the initial value would be + <current country phone code>, for example +1 for US.
/// If false it would be just +.
public var setCurrentCountryInitially: Bool = true { didSet { country = setCurrentCountryInitially ? Country.currentCountry : nil }}

/// Set to true for languages where flag and + must be at the right. For example for Arabic.
public var rightToLeftOrientation: Bool = false { didSet { presenter.isRightToLeftMode(on: rightToLeftOrientation) } }

Expand All @@ -63,6 +67,8 @@ open class NKVPhonePickerTextField: TextFieldPatternFormat {
set {
if let newValue = newValue {
presenter.setCountry(source: NKVSource(country: newValue))
} else {
presenter.setCountry(source: nil)
}
}
}
Expand Down Expand Up @@ -93,6 +99,22 @@ open class NKVPhonePickerTextField: TextFieldPatternFormat {
presenter.setFlag(source: source)
}

/// If you want to fill the number initially, use this method.
///
/// - Parameters:
/// - source: source country for formatting
/// - number: number (only digits)
public func preFillText(source: NKVSource,
number: Int) {
guard let c = Country.country(for: source) else {
print("⚠️ NKVPhonePickerTextField -> Can't prefill text with selected source.")
return
}
presenter.setFlag(source: source)
presenter.enablePhoneFormat(for: c)
text = "\(number)"
}

// MARK: - Customizing

// MARK: Country Picker
Expand Down Expand Up @@ -159,8 +181,8 @@ open class NKVPhonePickerTextField: TextFieldPatternFormat {

presenter.isRightToLeftMode(on: false)

if let countryForCurrentPhoneLocalization = Country.currentCountry {
country = countryForCurrentPhoneLocalization
if setCurrentCountryInitially {
country = Country.currentCountry
}
}

Expand Down

0 comments on commit 011f709

Please sign in to comment.