Numeric TextFields in SwiftUI

Поділитися
Вставка
  • Опубліковано 24 січ 2025

КОМЕНТАРІ • 69

  • @degisner
    @degisner Рік тому +1

    Stewart, your channel is one of the most informative on UA-cam about SwiftUI. I'm so glad when I search something and see your content in the results 👍

  • @DatafoxSoftware
    @DatafoxSoftware 2 роки тому +2

    Thanks Stewart, excellent solution. I found that TextField("Credit Limit", value: $supplierCreditLimit, format: .number) can works. Even you can type more chars than numbers the format to number will takes care of those chars, same if the user adds more than one decimal point.

  • @holycrosscv
    @holycrosscv 2 роки тому +2

    Love this! I need this all the time, and your solution is SO MUCH more elegant than mine! Thanks for another great video!

  • @samuelevans9115
    @samuelevans9115 9 місяців тому

    Stewart, you make the most informative videos for swiftui I have ever seen. Thanks!

    • @StewartLynch
      @StewartLynch  9 місяців тому +1

      Thank you Samuel. I appreciate the feedback

  • @mhfs61
    @mhfs61 Рік тому +1

    As stated before this video was very helpful.
    With your video as a stepping stone, I was able to solve an issue I had for while regarding the display of decimal and currency numbers.
    I expanded your code to limit the number of digits before and after the decimal separator. I also prevented the entry of the decimal separator or a zero as the first character.
    🙏🏼🙏🏼🙏🏼

    • @StewartLynch
      @StewartLynch  Рік тому

      Check out my SwiftUI workshop and the How Much application. I do that there too but differently from this tutorial

  • @ColeDano
    @ColeDano 8 місяців тому

    How is it that anytime I need to figure out something, Stewart is there? Very helpful again, thanks. Time to buy you another coffee :)

  • @rasheshbosamiya1482
    @rasheshbosamiya1482 2 роки тому +1

    Thank you for sharing knowledge, Stewart!

  • @DavidKoontz
    @DavidKoontz 2 роки тому +1

    Great technique! I’m going to use that tomorrow! I have attempted my own but it’s not quite as clean as yours.
    One thing I wanted was to swap the clear button to leading and the numbers to trailing alignment. More like most calculators display.

  • @VaughnWarriner
    @VaughnWarriner Рік тому

    Thanks!

    • @StewartLynch
      @StewartLynch  Рік тому +1

      Wow Vaughn. Thank you so much for this. Let me know if I can be of any assistance.

    • @VaughnWarriner
      @VaughnWarriner Рік тому

      @@StewartLynch Maybe you can. Do you have a simple method of resigning a datePicker field? The way mine works now I have to click outside the datefields to resign them, I have two in a row so it's not very user friendly. I have tried a bunch of different tips but nothing has worked in my situation.

    • @StewartLynch
      @StewartLynch  Рік тому

      I do have something that might work for you if you are just displaying the Date and not the time. If you email me at slynch@createchsol.com we can pursue this and I can send you want I have.

  • @Mahadev-x7u
    @Mahadev-x7u 2 роки тому +1

    Excellent and useful lesson... like always. Thank you Stewart. Happy New Year! 🎄

  • @oscarmartinez-pc1nj
    @oscarmartinez-pc1nj Рік тому

    this is great can't wait to see more of your videos.

  • @edsamour5046
    @edsamour5046 11 місяців тому

    Stewart thanks for your videos. You are an incredible teacher

  • @gustavomonge1785
    @gustavomonge1785 2 роки тому +1

    Thank you very much. A very clean a pro solution.

  • @mhfs61
    @mhfs61 Рік тому

    Very helpful. I always learn something new.
    The textfield clear button solution is great. I created my own solution but it didn't work to my satisfaction when using textfields in lists or forms.
    Thank you Stewart. 👍🏼👍🏼👍🏼

  • @andresraigoza2082
    @andresraigoza2082 2 роки тому +1

    Awesome! Thank you so much. Great tips

  • @ThomasSchmidt-v1g
    @ThomasSchmidt-v1g Рік тому

    Thanks Stewart. Excellent Video with a perfect solution for my keyboard problem.

  • @lukassbeataddicts
    @lukassbeataddicts 2 роки тому +1

    Nice one Steward. I actually has to build my own numberpad and used some custom logic for numbers to appear from right to left and placeholder to be 0.00

  • @dev98029
    @dev98029 Рік тому

    Thank you - your video just saved me hours of time.

  • @gakkieNL
    @gakkieNL 2 роки тому +1

    Great video as always. I do use a similar setup in my app 👍

  • @rahuljamba5846
    @rahuljamba5846 2 роки тому +1

    Very unique topic!!

  • @jean-claudegolovine5725
    @jean-claudegolovine5725 7 місяців тому

    Very good indeed. Many thanks!

  • @gjermundification
    @gjermundification 2 роки тому +1

    8:17 This is a very complex matter, while mathematics( and most data level storage ) uses . as decimal separator and , as separator for different values; let's call this `and` in which makes perfect sense for 2D coordinates and tuples, however for n-tuples `and` may be a bad word. Presenting data is quite simple with this in mind as data stored ( 59.10101010, 10.10101010 ) could be presented in any flavor. Parsing according to locale takes for granted that the device is personal, in which may make sense for 95% of users.

  • @zackf7140
    @zackf7140 Рік тому

    Awesome job! Helped me out a ton, thank you so much! 🔥⛺

  • @nileshjdarji
    @nileshjdarji 11 місяців тому

    I spent like over a day online and stack overflow to determine on how to format a textfield to accept decimals with two digits. This is awesome. You rescued me. This is the only video that shows this. Thank you for putting this out. There is one more thing I would like to ask. If my textfield is for currency, how do I make $ symbol appear at the beginning of the number that I enter. Or is this something that I need to do when I query swiftdata to get data and format the textview to show the dollar sign. Please guide me if you can. Thank you.

    • @StewartLynch
      @StewartLynch  11 місяців тому +1

      Unfortunately this method will not work for that as it will strip out the currency symbol.

  • @drmucahiderdogan
    @drmucahiderdogan 2 роки тому +1

    Great video!

  • @VaughnWarriner
    @VaughnWarriner Рік тому +1

    Resolved. It works perfectly,
    I followed everything to the letter, iOS 16.4 and Xcode don't complain and the keyboards come up but with two exceptions 1) it would not compile with the spacer() on the toolbar, something about format space and 2) the decimal keypad will still allow additional decimals for some reason. I have run this in simulator and on my iPhone 13 Pro. If you have any suggestions for me other than give up coding, please let me know. Maybe someone else has had this situation come up.

    • @StewartLynch
      @StewartLynch  Рік тому

      Have you downloaded the completed project from the link in the description. I just did and it compiled and ran fine so I suggest you do that and compare your code. I am running on Simulator and iPhone 13 Pro too. You can also email me your project as a zip file and I will take a look, but it would likely stick more if you can compare to the completed project and find your own error.

    • @VaughnWarriner
      @VaughnWarriner Рік тому

      @@StewartLynch My textfield is not $intNumberString and $decNumberString but rather $inputText2 and $inputText3 which are user input and saved with a button. So I think I need to create additional or change @State private var intNumberString = "" accordingly. Thank you for responding by the way. I am an old retired Bell System power engineer NOT a coder, my fascination with coding started with Kernigan and Ritchie and the creation of Unix and I should have jumped in at that point, haha. I will let you know if I can get it to work correctly.

    • @VaughnWarriner
      @VaughnWarriner Рік тому

      Of course $inputText2 and $inputText3 were already initialized so I put those in place of $intNumberString and $decNumberString and it works PERFECT. You are the man and thank you again.

    • @StewartLynch
      @StewartLynch  Рік тому

      Great news Vaughn. Glad it worked out for you.

  • @cruiseqe2
    @cruiseqe2 2 роки тому +1

    Awesome video. I have tried to 'engineer' my own solution, but it was not as slick as your version.
    I often like to restrict my users to entering amounts, and so would like to limit the input to a maximum of 2 decimal digits. (I know that some countries have 3 decimals in their amounts - but I am conveniently ignoring those at the moment). I guess that I could just adjust the Combine section in your code?

  • @chrisstromberg6527
    @chrisstromberg6527 Рік тому

    Very helpful. I'm curious, why doesn't Apple incorporate this functionality when using text fields and numbers?

    • @StewartLynch
      @StewartLynch  Рік тому

      That would be a question for Apple.

    • @chrisstromberg6527
      @chrisstromberg6527 Рік тому

      @@StewartLynch I agree, thanks for another great instructional video!

  • @HaKaNOGUZ
    @HaKaNOGUZ Рік тому

    Hi Stewart. Thanks for the great video. I have one question. When i run the simulator, comma is the decimap separator but I want period to be the seperator. I change the region to US and number format but still comma appears as a decimal seperator. Please help me solve this problem.

  • @jmd62800
    @jmd62800 11 місяців тому

    Very well ❤❤👏👏👏👏👏

  • @warrenscorner
    @warrenscorner Рік тому

    Fantastic! I will use this in my Stepper Motor Calculator app. I'm a newbie and the code for the iPad is very complicated to me. I'll just restrict my app to an iPhone. It's just for my own personal use. Do you know of a way to install it on my iPhone without it disappearing after a day or so? Thank you.

    • @StewartLynch
      @StewartLynch  Рік тому

      Get a developer account. They are free now until you want to publish to the App Store.

  • @vloggymcvlogface
    @vloggymcvlogface 4 місяці тому

    Thanks Stewart. Loved the video. I have not been able to get the .clearbuttonMode to work when using axis: .vertical parameter. Does the axis parameter mean that the textfield no longer conforms to UITextField?

    • @StewartLynch
      @StewartLynch  4 місяці тому

      I just downloaded the completed project from this video and updated the deployment target to iOS 18 and using Xcode 16, I could see no issue. The clear button still works.

    • @vloggymcvlogface
      @vloggymcvlogface 4 місяці тому

      @@StewartLynchThanks for the speedy reply! I downloaded the completed project and using my minimum deployment iOS16.2 and XCode 15.4, the clear button works as you described/intended. However, when I changed the textfield line from:
      TextField("Enter Integer Number", text: $intNumberString), to
      TextField("Enter Integer Number", text: $intNumberString), axis: .vertical)
      the clear button (the image "multiply.circle.fill") does not appear.

    • @StewartLynch
      @StewartLynch  4 місяці тому

      @@vloggymcvlogface OK. You are right. That breaks it. You could remove that line in the view, and surround the Content in the view modifier with an HStack and add your own clear button there.
      HStack {
      content
      Spacer()
      if !text.isEmpty {
      Button {
      self.text = ""
      } label: {
      Image(systemName: "xmark.circle.fill")
      }
      .foregroundStyle(.secondary)
      }
      }

    • @vloggymcvlogface
      @vloggymcvlogface 4 місяці тому

      @@StewartLynch With a small modification your suggestion works just fine:
      1. the xmark did not go away when the field was not in focus, so I created a new FocusState boolean which I passed to the viewModifier to set the opacity of the xmark to 0 when the field did not have focus.
      Also I tried changing the HStack to a ZStack to get the xmark inside the textfield but I couldn't figure out how to prevent the inputted text from overwriting the xmark. An issue for another time...
      Thanks for your help and I love your other videos. Also thanks for suggesting Fireside Swift - I have listened to all their podcasts
      FWIW I wrote my code in FortranIV in 1970 and I still love coding.
      Cheers
      John

    • @StewartLynch
      @StewartLynch  4 місяці тому

      @vloggymcvlogface what I don’t really understand is why you would need a vertical access for a numeric text entry. Would it not always be on a single line?

  • @IM-br1eb
    @IM-br1eb Рік тому

    Brilliant

  • @Bankai3474
    @Bankai3474 10 місяців тому

    Is there a way to have the commas be added while entering a numeric value? Also, check when the user is entering in a decimal number larger than 100? I currently have an alert for this based on what the value being entered in is but feels annoying in using the app.

    • @StewartLynch
      @StewartLynch  10 місяців тому

      This might be possible., but not likely with this method.

  • @pierremarais7669
    @pierremarais7669 Рік тому

    Thanks for all your videos, I do learn a lot from your work. I have the following question please; first I am new to Swiftui, I using a Zstack to build a word game where I create a 'clock-like' (1-12), I did had a textfield so that the user can enter 12 characters and it must goes as follows, on 'the clock' 1=A, 2=B, 3=C and so on. But I want to the text updates while user enters the characters in the textfield, I play around with the .onreceive to be able to collect only the last character enetered and place the character at it position, where can I find anything wrt this please, thanks Pierre

  • @aaryanmakwana515
    @aaryanmakwana515 Рік тому

    does FocusedFiled not work on macOS? because the error says its out of scope

  • @UncensoredCowboy
    @UncensoredCowboy 10 місяців тому

    How do you make it auto input commas for the thousands, millions, billions denotations to make the numbers easier to read.

  • @larschristiansen3136
    @larschristiansen3136 2 роки тому +1

    Great Video, removing pain from managing keyboard and text fields with numerals.
    I discovered that Locale.current.decimalSeparator has disappeared from Ventura.
    Rather you cannot find it anymore in the system prefs panes.
    On my French version of Ventura 13.1 I have the right separator "," but Locale.current.decimalSeparator persists to present the current one as ".".

  • @TuanHoang-qj3yq
    @TuanHoang-qj3yq Рік тому

    Can this be used in TextField(“amount”, value: $amount, format: .currency(code:Locale.current.currency ?? “USD”))
    I tried to change @Binding var text: String to @Binding var num: Double
    And other changes such as String(num).first() then String(num).dropLast() etc
    It works but unlike your example, the textfield still shows every character that is typed in. Even though all the characters except “1234567890.” are cleared after the Return key is hit, I am still trying to figure out the way to make it behaves like the way your example behaves, i.e any character being not one of those “1234567890.” will not show.
    I very much appreciate any suggestions. Thank you in advance.

    • @StewartLynch
      @StewartLynch  Рік тому

      No. A textfield is bound to a String, whereas the format is bound to a decimal value. I don't quite understand what your issues are.