This is a problem many SwiftUI developers are struggling with. Why doesn't SwiftUI
have a text field for money input? Well finally we have an answer. Say hello to CurrencyField.
The missing SwiftUI text field for handling currencies. Make sure you SUBSCRIBE to that channel
and smash that LIKE button and let's see how to create a text field that handles currencies.
Let me show you how powerful CurrencyField is. And we are going to see this inside one of my simulators.
It's an iOS simulator but currency field works really, really well on macOS, iOS, tvOS, watchOS.
Of course all of the Apple devices. So this is how it looks like, this is the demo project actually
that you are going to get access to once you take a look at CurrencyField and yeah this is what
we add in here. Let's add in some sense right over here and if I try to tap more as you can
see it's not going to add more as expected and if I delete here you can see it's just deleting the
dollar sign also. And of course here are the tremendous ways that you can customize this.
Let's just have some tip right over here, point 80 and we can also delete that. So this is what we
are going to build out today. Go ahead and check out CurrencyField it's at: STORE.REBELOPER.COM/CURRENCYFIELD.
Now what does CurrencyField do? Well it's a text field for handling currencies
and it's really, really awesome. It works on all of the devices or platforms that I have
told you about. It's a setup is a free simple step setup. It has a currency field
style modifier if appliable for that platform of course and really, really great thing is that.
It converts the text field string value into a double. So you can use that value in your code
furthermore maybe just send it to a server. Also it adds and removes that currency symbol and
the to end ad from the text field and it has built-in currencies that you can select from
otherwise you just add in your string value. It automatically adds in the decimal number
pad keyboard and limits the decimals to count off to build on SwiftUI 5.6. It has an example
project and it's 100% documented. And if you want to take a look at the steps and some really
nice screenshots with all of the platforms go ahead and check out it's at STORE.REBELOPER.COM/CURRENCYFIELD.
Now let's take a look at our example. So what I'm going to do is open up my Xcode.
Here is my dummy project, well demo project actually and what I have done is just added an example view
that I'm going to present right over here instead of the content view. The content view
has all of the text fields, well currency fields that are actually text fields inside itself
that you have already seen and yeah we have a simple hello world. Let's just build and run right
over here so we can see that everything works as accepted, there we go. And what we are going to do
is add a currency field. Now all you have to do is just add in the CurrencyField
library that you have downloaded after your purchase and then you can just
set this up in a few easy steps. Now first one is creating a private state object and let's just
name this currency and there are a lot of ways that you can set up your currency. Let's just
see one way of doing so, as you can see we can add in a string value a currency type. We can add also
optionally a title and the title foreground color. So let's just go with a simple one with a type
Let's add a dot right over here and you can see all of the default ones here. Let's just choose
Euro here and that's it we have our currency set up. Now we have to add in our CurrencyField.
So just type out currency field open parentheses and add in your currency and believe it or not that
is all you have to do right over here. I optionally add also a currency field style of rounded
border because I think it looks really, really nice. By the way you can just customize it
to your needs. As you can see in the content view we have a lot of customization
options right over here for this currency field we added a font, foreground color, padding, background.
You know it's all SwiftUI, okay. So let's see how this looks like. Let's build and run
and see our currency, there we go. We have our Euro sign right over here. If we tap over there we
have 25 point maybe 54 and if I try to add nothing is added furthermore which is really, really great.
And we can just delete that. It's really nice. By the way I do like
the one with Euro with space, so it adds a space after the the symbol and let's just add instead
of hello world let's grab our currency's value. So the currency value is actually a double.
So what we need to do is a string interpolation right over here and grab the currency that we
have created and add a dot and you will see that we have a value right over here.
So let's build and run again and let's see if that value is added right over here. Yes! It is.
So let's add a 12 right over here and the dot maybe 78 and as you can see it's automatically
updating, it's really, really cool, okay. So let's see what other options do we have. Of course
all of this works and I will just add and back in my content view right over here, it also works
on all OSs so as you can see I just added here some if, EndIf delimiters because for example
the currency field of rounded border is not available on tvOS and watchOS. This is a SwiftUI
thing of course. And let's see what we added here is a currency field style which we already saw
there, by the way just check out how awesome these initializers are. You can have a Bitcoin with space.
Swift coin just a simple string. USD, we can add a title of price and also a foreground color of
white, okay. So let's just add it right over here. Let's build in front and let's see how this looks
like for example. One, five, there we go. It's really, really great as you can see. It is really, really
customizable. Let's take a look at this price also, there we go. If we delete and Swift coin maybe,
We bought some Swift coin that's really imaginary right over here and some Bitcoin, there we go. 25
maybe 0-0 and that's it. You saw the dollar currency field right over here. Now all of this is
inside the demo project and you can purchase this at STORE.REBELOPER.COM/CURRENCYFIELD. Ss you can
see setting up a currency field with CurrencyField is really, really straightforward and if you want
to learn about the 6 senior developer habits go ahead and check out the video right over here.