Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Up to date #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ struct ContentView: View {
$0.numberPlaceholderColor = .label
}
.padding()
.background(
RoundedRectangle(cornerRadius: 15)
.fill(Color(.secondarySystemGroupedBackground))
)
.padding()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
2AA44DC328A11687001BF8F0 /* MultiplatformDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MultiplatformDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
2AA44DC328A11687001BF8F0 /* iPhoneNumberFieldDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iPhoneNumberFieldDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
2AA44DC628A11687001BF8F0 /* MultiplatformDemoApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiplatformDemoApp.swift; sourceTree = "<group>"; };
2AA44DC828A11687001BF8F0 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
2AA44DCA28A11688001BF8F0 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -49,7 +49,7 @@
2AA44DC428A11687001BF8F0 /* Products */ = {
isa = PBXGroup;
children = (
2AA44DC328A11687001BF8F0 /* MultiplatformDemo.app */,
2AA44DC328A11687001BF8F0 /* iPhoneNumberFieldDemo.app */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -92,9 +92,9 @@
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
2AA44DC228A11687001BF8F0 /* MultiplatformDemo */ = {
2AA44DC228A11687001BF8F0 /* iPhoneNumberFieldDemo */ = {
isa = PBXNativeTarget;
buildConfigurationList = 2AA44DD228A11688001BF8F0 /* Build configuration list for PBXNativeTarget "MultiplatformDemo" */;
buildConfigurationList = 2AA44DD228A11688001BF8F0 /* Build configuration list for PBXNativeTarget "iPhoneNumberFieldDemo" */;
buildPhases = (
2AA44DBF28A11687001BF8F0 /* Sources */,
2AA44DC028A11687001BF8F0 /* Frameworks */,
Expand All @@ -104,12 +104,12 @@
);
dependencies = (
);
name = MultiplatformDemo;
name = iPhoneNumberFieldDemo;
packageProductDependencies = (
2AA44DD828A116D8001BF8F0 /* iPhoneNumberField */,
);
productName = MultiplatformDemo;
productReference = 2AA44DC328A11687001BF8F0 /* MultiplatformDemo.app */;
productReference = 2AA44DC328A11687001BF8F0 /* iPhoneNumberFieldDemo.app */;
productType = "com.apple.product-type.application";
};
/* End PBXNativeTarget section */
Expand All @@ -127,7 +127,7 @@
};
};
};
buildConfigurationList = 2AA44DBE28A11687001BF8F0 /* Build configuration list for PBXProject "MultiplatformDemo" */;
buildConfigurationList = 2AA44DBE28A11687001BF8F0 /* Build configuration list for PBXProject "iPhoneNumberFieldDemo" */;
compatibilityVersion = "Xcode 14.0";
developmentRegion = en;
hasScannedForEncodings = 0;
Expand All @@ -140,7 +140,7 @@
projectDirPath = "";
projectRoot = "";
targets = (
2AA44DC228A11687001BF8F0 /* MultiplatformDemo */,
2AA44DC228A11687001BF8F0 /* iPhoneNumberFieldDemo */,
);
};
/* End PBXProject section */
Expand Down Expand Up @@ -300,12 +300,12 @@
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 14;
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 12.5;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.example.iphonetextfield.MultiplatformDemo;
PRODUCT_BUNDLE_IDENTIFIER = com.example.iphonetextfield.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = auto;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
Expand Down Expand Up @@ -336,12 +336,12 @@
"INFOPLIST_KEY_UIStatusBarStyle[sdk=iphonesimulator*]" = UIStatusBarStyleDefault;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
IPHONEOS_DEPLOYMENT_TARGET = 14;
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 12.5;
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.example.iphonetextfield.MultiplatformDemo;
PRODUCT_BUNDLE_IDENTIFIER = com.example.iphonetextfield.demo;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = auto;
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
Expand All @@ -354,7 +354,7 @@
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
2AA44DBE28A11687001BF8F0 /* Build configuration list for PBXProject "MultiplatformDemo" */ = {
2AA44DBE28A11687001BF8F0 /* Build configuration list for PBXProject "iPhoneNumberFieldDemo" */ = {
isa = XCConfigurationList;
buildConfigurations = (
2AA44DD028A11688001BF8F0 /* Debug */,
Expand All @@ -363,7 +363,7 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
2AA44DD228A11688001BF8F0 /* Build configuration list for PBXNativeTarget "MultiplatformDemo" */ = {
2AA44DD228A11688001BF8F0 /* Build configuration list for PBXNativeTarget "iPhoneNumberFieldDemo" */ = {
isa = XCConfigurationList;
buildConfigurations = (
2AA44DD328A11688001BF8F0 /* Debug */,
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct ContentView: View {
<br/>

<h3 align="left">Focus and unfocus 🔍</h3>
<p align="left">Use iPhoneNumberField's optional binding and <a href="https://github.com/benjaminsage/iTextField"> iTextField</a> to programmatically change text field.</p>
<p align="left">Use iPhoneNumberField's optional binding and programmatically change the text field.</p>

<img align="left" src="/Resources/example-2.gif" width="250">

Expand All @@ -94,10 +94,8 @@ struct ContentView: View {

var body: some View {
VStack {
iTextField("Name", text: $nameText)
.font(UIFont(size: 24, weight: .light, design: .monospaced))
TextField("Name", text: $nameText)
.padding()
.onReturn { phoneEditing = true }
iPhoneNumberField("Phone", text: $phoneText, isEditing: $phoneEditing)
.font(UIFont(size: 24, weight: .light, design: .monospaced))
.padding()
Expand Down Expand Up @@ -222,11 +220,11 @@ dependencies: [
```

# 🌟 Become a Paid Subscriber
This library is under the **MIT** license and completely **FREE**. Anyone can use it anywhere and any contribution is wellcomed.
This library is under the **MIT** license and completely **FREE**. Anyone can use it anywhere, and any contribution is welcome.

In addition, you can show your support and [become My sponsor](https://www.buymeacoffee.com/mojtabahs/membership).
By supporting me, you will gain¹:
- Tickets for code level support in any project you want (not just this one)
- Tickets for code-level support in any project you want (not just this one)
- Access me through my socials and we can discuss technologies together.
- Discounts and early access to my premium products.
- Early access to updates and bug fixes.
Expand Down
129 changes: 71 additions & 58 deletions Sources/iPhoneNumberField/iPhoneNumberField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,49 +159,50 @@ public struct iPhoneNumberField: UIViewRepresentable {
uiView.partialFormatter.defaultRegion = defaultRegion
}

configuration(uiView)
return uiView
}

public func updateUIView(_ uiView: PhoneNumberTextField, context: UIViewRepresentableContext<Self>) {
uiView.textContentType = .telephoneNumber //allow auto-fill to work with telephone text field
uiView.text = text
uiView.font = font
uiView.maxDigits = maxDigits
uiView.clearButtonMode = clearButtonMode
uiView.placeholder = placeholder
uiView.borderStyle = borderStyle
uiView.textColor = textColor
uiView.withFlag = showFlag
uiView.withDefaultPickerUI = selectableFlag
uiView.withPrefix = previewPrefix
uiView.partialFormatter.defaultRegion = defaultRegion ?? PhoneNumberKit.defaultRegionCode()
if placeholder != nil {
DispatchQueue.main.async {
uiView.textContentType = .telephoneNumber //allow auto-fill to work with telephone text field
uiView.text = text
uiView.font = font
uiView.maxDigits = maxDigits
uiView.clearButtonMode = clearButtonMode
uiView.placeholder = placeholder
} else {
uiView.withExamplePlaceholder = autofillPrefix
}

uiView.tintColor = accentColor

if let numberPlaceholderColor = numberPlaceholderColor {
uiView.numberPlaceholderColor = numberPlaceholderColor
}
if let countryCodePlaceholderColor = countryCodePlaceholderColor {
uiView.countryCodePlaceholderColor = countryCodePlaceholderColor
}
if let textAlignment = textAlignment {
uiView.textAlignment = textAlignment
}
uiView.borderStyle = borderStyle
uiView.textColor = textColor
uiView.withFlag = showFlag
uiView.withDefaultPickerUI = selectableFlag
uiView.withPrefix = previewPrefix
uiView.partialFormatter.defaultRegion = defaultRegion ?? PhoneNumberKit.defaultRegionCode()
if placeholder != nil {
uiView.placeholder = placeholder
} else {
uiView.withExamplePlaceholder = autofillPrefix
}

uiView.tintColor = accentColor

if let numberPlaceholderColor = numberPlaceholderColor {
uiView.numberPlaceholderColor = numberPlaceholderColor
}
if let countryCodePlaceholderColor = countryCodePlaceholderColor {
uiView.countryCodePlaceholderColor = countryCodePlaceholderColor
}
if let textAlignment = textAlignment {
uiView.textAlignment = textAlignment
}

DispatchQueue.main.async {
if isFirstResponder {
uiView.becomeFirstResponder()
} else {
uiView.resignFirstResponder()
}

configuration(uiView)
}

configuration(uiView)
}

public func makeCoordinator() -> Coordinator {
Expand Down Expand Up @@ -256,53 +257,65 @@ public struct iPhoneNumberField: UIViewRepresentable {
var onReturn = { (view: PhoneNumberTextField) in }

@objc public func textViewDidChange(_ textField: UITextField) {
guard let textField = textField as? PhoneNumberTextField else {
return assertionFailure("Undefined state")
}

// Updating the binding
if formatted {
// Display the text exactly if unformatted
text.wrappedValue = textField.text ?? ""
} else {
if let number = textField.phoneNumber {
// If we have a valid number, update the binding
let country = String(number.countryCode)
let nationalNumber = String(number.nationalNumber)
text.wrappedValue = "+" + country + nationalNumber
DispatchQueue.main.async { [weak self] in
guard let self else { return }

guard let textField = textField as? PhoneNumberTextField else {
return assertionFailure("Undefined state")
}

// Updating the binding
if formatted {
// Display the text exactly if unformatted
text.wrappedValue = textField.text ?? ""
} else {
// Otherwise, maintain an empty string
text.wrappedValue = ""
if let number = textField.phoneNumber {
// If we have a valid number, update the binding
let country = String(number.countryCode)
let nationalNumber = String(number.nationalNumber)
text.wrappedValue = "+" + country + nationalNumber
} else {
// Otherwise, maintain an empty string
text.wrappedValue = ""
}
}

displayedText.wrappedValue = textField.text ?? ""
onEditingChange(textField)
onPhoneNumberChange(textField.phoneNumber)
}

displayedText.wrappedValue = textField.text ?? ""
onEditingChange(textField)
onPhoneNumberChange(textField.phoneNumber)
}

public func textFieldDidBeginEditing(_ textField: UITextField) {
DispatchQueue.main.async { [isFirstResponder] in
DispatchQueue.main.async { [weak self] in
guard let self else { return }
isFirstResponder.wrappedValue = true
onBeginEditing(textField as! PhoneNumberTextField)
}
onBeginEditing(textField as! PhoneNumberTextField)
}

public func textFieldDidEndEditing(_ textField: UITextField) {
DispatchQueue.main.async { [isFirstResponder] in
DispatchQueue.main.async { [weak self] in
guard let self else { return }
isFirstResponder.wrappedValue = false
onBeginEditing(textField as! PhoneNumberTextField)
}
onEndEditing(textField as! PhoneNumberTextField)
}

public func textFieldShouldClear(_ textField: UITextField) -> Bool {
displayedText.wrappedValue = ""
onClear(textField as! PhoneNumberTextField)
DispatchQueue.main.async { [weak self] in
guard let self else { return }
displayedText.wrappedValue = ""
onClear(textField as! PhoneNumberTextField)
}
return true
}

public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
onReturn(textField as! PhoneNumberTextField)
DispatchQueue.main.async { [weak self] in
guard let self else { return }
onReturn(textField as! PhoneNumberTextField)
}
return true
}
}
Expand Down