Skip to content

Commit 16be642

Browse files
committed
Add option to specify currency symbol. Updated documentation
1 parent c4babb9 commit 16be642

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ struct ContentView: View {
3737
@State private var value = 0.0
3838

3939
var body: some View {
40+
//Minimal configuration
4041
CurrencyTextField("Amount", value: self.$value)
42+
43+
//All configurations
44+
CurrencyTextField("Amount", value: self.$value, alwaysShowFractions: false, numberOfDecimalPlaces: 2, currencySymbol: "US$")
4145
.font(.largeTitle)
4246
.multilineTextAlignment(TextAlignment.center)
4347
}

Sources/SwiftUIKit/views/CurrencyTextField.swift

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public struct CurrencyTextField: UIViewRepresentable {
1515
private var tag: Int
1616
private var alwaysShowFractions: Bool
1717
private var numberOfDecimalPlaces: Int
18+
private var currencySymbol: String?
1819

1920
private var placeholder: String
2021

@@ -46,6 +47,7 @@ public struct CurrencyTextField: UIViewRepresentable {
4647
tag: Int = 0,
4748
alwaysShowFractions: Bool = false,
4849
numberOfDecimalPlaces: Int = 2,
50+
currencySymbol: String? = nil,
4951
font: UIFont? = nil,
5052
foregroundColor: UIColor? = nil,
5153
accentColor: UIColor? = nil,
@@ -67,6 +69,7 @@ public struct CurrencyTextField: UIViewRepresentable {
6769
self.tag = tag
6870
self.alwaysShowFractions = alwaysShowFractions
6971
self.numberOfDecimalPlaces = numberOfDecimalPlaces
72+
self.currencySymbol = currencySymbol
7073

7174
self.font = font
7275
self.foregroundColor = foregroundColor
@@ -94,7 +97,7 @@ public struct CurrencyTextField: UIViewRepresentable {
9497

9598
// initial value
9699
if let v = self.value {
97-
textField.text = v.currencyFormat(decimalPlaces: self.numberOfDecimalPlaces, forceShowDecimalPlaces: self.alwaysShowFractions)
100+
textField.text = v.currencyFormat(decimalPlaces: self.numberOfDecimalPlaces, forceShowDecimalPlaces: self.alwaysShowFractions, currencySymbol: self.currencySymbol)
98101
}
99102

100103
// tag
@@ -178,7 +181,7 @@ public struct CurrencyTextField: UIViewRepresentable {
178181
}
179182

180183
public func makeCoordinator() -> CurrencyTextField.Coordinator {
181-
Coordinator(value: $value, isResponder: self.isResponder, alwaysShowFractions: self.alwaysShowFractions, numberOfDecimalPlaces: self.numberOfDecimalPlaces, onReturn: self.onReturn){ flag in
184+
Coordinator(value: $value, isResponder: self.isResponder, alwaysShowFractions: self.alwaysShowFractions, numberOfDecimalPlaces: self.numberOfDecimalPlaces, currencySymbol: self.currencySymbol, onReturn: self.onReturn){ flag in
182185
self.onEditingChanged(flag)
183186
}
184187
}
@@ -190,7 +193,7 @@ public struct CurrencyTextField: UIViewRepresentable {
190193
if self.value == nil {
191194
textField.text = nil
192195
} else {
193-
textField.text = self.value!.currencyFormat(decimalPlaces: self.numberOfDecimalPlaces, forceShowDecimalPlaces: self.alwaysShowFractions)
196+
textField.text = self.value!.currencyFormat(decimalPlaces: self.numberOfDecimalPlaces, forceShowDecimalPlaces: self.alwaysShowFractions, currencySymbol: self.currencySymbol)
194197
}
195198
}
196199

@@ -216,6 +219,8 @@ public struct CurrencyTextField: UIViewRepresentable {
216219
private var onReturn: ()->()
217220
private var alwaysShowFractions: Bool
218221
private var numberOfDecimalPlaces: Int
222+
private var currencySymbol: String?
223+
219224
var internalValue: Double?
220225
var onEditingChanged: (Bool)->()
221226
var didBecomeFirstResponder = false
@@ -224,6 +229,7 @@ public struct CurrencyTextField: UIViewRepresentable {
224229
isResponder: Binding<Bool>?,
225230
alwaysShowFractions: Bool,
226231
numberOfDecimalPlaces: Int,
232+
currencySymbol: String?,
227233
onReturn: @escaping () -> Void = {},
228234
onEditingChanged: @escaping (Bool) -> Void = { _ in }
229235
) {
@@ -233,6 +239,7 @@ public struct CurrencyTextField: UIViewRepresentable {
233239
self.isResponder = isResponder
234240
self.alwaysShowFractions = alwaysShowFractions
235241
self.numberOfDecimalPlaces = numberOfDecimalPlaces
242+
self.currencySymbol = currencySymbol
236243
self.onReturn = onReturn
237244
self.onEditingChanged = onEditingChanged
238245
}
@@ -242,7 +249,7 @@ public struct CurrencyTextField: UIViewRepresentable {
242249
let originalText = textField.text
243250
let text = textField.text as NSString?
244251
let newValue = text?.replacingCharacters(in: range, with: string)
245-
let display = newValue?.currencyFormat(decimalPlaces: self.numberOfDecimalPlaces)
252+
let display = newValue?.currencyFormat(decimalPlaces: self.numberOfDecimalPlaces, currencySymbol: self.currencySymbol)
246253

247254
// validate change
248255
if !shouldAllowChange(oldValue: textField.text ?? "", newValue: newValue ?? "") {
@@ -323,7 +330,7 @@ public struct CurrencyTextField: UIViewRepresentable {
323330
}
324331

325332
public func textFieldDidEndEditing(_ textField: UITextField) {
326-
textField.text = self.value?.currencyFormat(decimalPlaces: self.numberOfDecimalPlaces, forceShowDecimalPlaces: self.alwaysShowFractions)
333+
textField.text = self.value?.currencyFormat(decimalPlaces: self.numberOfDecimalPlaces, forceShowDecimalPlaces: self.alwaysShowFractions, currencySymbol: self.currencySymbol)
327334
DispatchQueue.main.async {
328335
self.isResponder?.wrappedValue = false
329336
}
@@ -390,7 +397,7 @@ fileprivate extension String {
390397

391398
// args:
392399
// decimalPlaces - the max number of decimal places
393-
func currencyFormat(decimalPlaces: Int? = nil) -> String? {
400+
func currencyFormat(decimalPlaces: Int? = nil, currencySymbol: String? = nil) -> String? {
394401
// uses self.double
395402
// logic for varying the number of fraction digits
396403
guard let double = double else {
@@ -417,6 +424,10 @@ fileprivate extension String {
417424
return formatted
418425
}
419426

427+
if currencySymbol != nil {
428+
formatter.currencySymbol = currencySymbol
429+
}
430+
420431
formatter.maximumFractionDigits = 0
421432
let formatted = formatter.string(from: NSNumber(value: double))
422433
return formatted
@@ -427,7 +438,7 @@ fileprivate extension Double {
427438
// args:
428439
// decimalPlaces - number of decimal places
429440
// forceShowDecimalPlaces - whether to force show fractions
430-
func currencyFormat(decimalPlaces: Int? = nil, forceShowDecimalPlaces: Bool = false) -> String? {
441+
func currencyFormat(decimalPlaces: Int? = nil, forceShowDecimalPlaces: Bool = false, currencySymbol: String? = nil) -> String? {
431442
let formatter = Formatter.currency
432443
var integer = 0.0
433444
let d = decimalPlaces != nil ? decimalPlaces! : 2
@@ -444,6 +455,10 @@ fileprivate extension Double {
444455
formatter.maximumFractionDigits = 0
445456
}
446457
}
458+
459+
if currencySymbol != nil {
460+
formatter.currencySymbol = currencySymbol
461+
}
447462

448463
return formatter.string(from: NSNumber(value: self))
449464
}

0 commit comments

Comments
 (0)