Skip to content

Commit

Permalink
Added var to make flag fixed.
Browse files Browse the repository at this point in the history
Fixed custom phone formatting setting.
  • Loading branch information
NikKovIos committed Mar 19, 2018
1 parent e410273 commit 455dcf9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Example/ExampleViewController.swift
Expand Up @@ -26,8 +26,11 @@ class ExampleViewController: UIViewController {
// topTextField.enablePlusPrefix = false

// Setting initial custom country
let country = Country.country(for: NKVSource(countryCode: "EG"))
let country = Country.country(for: NKVSource(countryCode: "RU"))
topTextField.country = country

// Setting to let the flag be changed only with code
// topTextField.isFlagFixed = true

// Setting custom format pattern for some countries
topTextField.customPhoneFormats = ["RU" : "# ### ### ## ##",
Expand Down
7 changes: 7 additions & 0 deletions Sources/Main/NKVPhonePickerPresenter.swift
Expand Up @@ -53,6 +53,13 @@ class NKVPhonePickerPresenter {
self.textField.setFormatting(country.formatPattern, replacementChar: "#")
}

/// Enable custom formats instantly when assigned
public func didSetCustomPhoneFormats() {
if let country = self.textField.country {
self.enablePhoneFormat(for: country)
}
}

/// If off - removing the plus prefix
public func plusPrefix(on: Bool) {
if !on {
Expand Down
11 changes: 8 additions & 3 deletions Sources/Main/NKVPhonePickerTextField.swift
Expand Up @@ -28,7 +28,7 @@ open class NKVPhonePickerTextField: TextFieldPatternFormat {
/// Ex:
///
/// textField.customPhoneFormats = ["RU" : "# ### ### ## ##", "GB": "# #### #########"]
public var customPhoneFormats: [String: String]?
public var customPhoneFormats: [String: String]? { didSet { presenter.didSetCustomPhoneFormats() } }

/// Set to 'false' if you don't need the '+' prefix to be visible
public var enablePlusPrefix: Bool = true { didSet { presenter.plusPrefix(on: enablePlusPrefix) } }
Expand All @@ -39,6 +39,9 @@ open class NKVPhonePickerTextField: TextFieldPatternFormat {
/// 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) } }

/// If true the flag icon and country can be changed only by code. For ex: topTextField.country = Country.country(for: NKVSource(countryCode: "EG"))
public var isFlagFixed: Bool = false

// MARK: - Get

/// Current selected country in TextField
Expand Down Expand Up @@ -243,7 +246,9 @@ open class NKVPhonePickerTextField: TextFieldPatternFormat {

extension NKVPhonePickerTextField: CountriesViewControllerDelegate {
public func countriesViewController(_ sender: CountriesViewController, didSelectCountry country: Country) {
self.country = country
if isFlagFixed == false {
self.country = country
}
}

open func countriesViewControllerDidCancel(_ sender: CountriesViewController) {
Expand All @@ -253,7 +258,7 @@ extension NKVPhonePickerTextField: CountriesViewControllerDelegate {

extension NKVPhonePickerTextField: UITextFieldDelegate {
@objc fileprivate func textFieldDidChange() {
if let newString = self.text {
if let newString = self.text, isFlagFixed == false {
if newString.count == 1 || newString.count == 0 {
self.setFlag(source: NKVSource(country: Country.empty))
}
Expand Down

0 comments on commit 455dcf9

Please sign in to comment.