Skip to content

Commit c296012

Browse files
authored
Updated readme to cover NumericTextModifier
1 parent fdaa3e3 commit c296012

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# NumericText
22

3-
![Build](https://github.com/amayers/NumericText/workflows/Swift/badge.svg)
4-
53
A simple SwiftUI `TextField` that limits user input to numbers.
64

75
The most common response in https://stackoverflow.com/questions/58733003/swiftui-how-to-create-textfield-that-only-accepts-numbers (the top search result), advocates just setting a numeric keyboard. But that totally misses cases when used with hardware keyboards. Many of the suggested
86

9-
This `NumericTextField` prevents any non-numeric text input, no matter the source (paste, external keyboard). You can choose to allow integers or floating point.
7+
This `NumericTextModifier` observes a `String` value for changes, and when it is changed, it filters out any non-numeric characters and updates the string. Then it converts that string to a `NSNumber` for easier use. You can choose to allow integers or floating point.
8+
9+
`NumericTextField` uses `NumbericTextModifier` and exposes only the `NSNumber` in the text. It prevents any non-numeric text input, no matter the source (paste, external keyboard). It also manages the keyboard type to match the type of numbers you said to allow.
10+
1011
Standard `TextFields` have a `Formatter` that you can pass in, that will be used to format/validate input. However this only occurs when the user finishes editing, not for every keystroke. So a user can type `123abc4` and see that in the text field, then when they hit return it will change to `1234`. That's really not ideal. With `NumericTextField` when they type a non-numeric character it is ignored and never shows up in the text field.
1112

1213

1314
## Usage:
1415

16+
### NumericTextField
17+
1518
It works just like `TextField` but you are binding it to `NSNumber?` instead of a `String`.
1619

1720
```
1821
// Inside your view
19-
@State private static var int: NSNumber?
20-
@State private static var double: NSNumber?
22+
@State private var int: NSNumber?
23+
@State private var double: NSNumber?
2124
2225
var body: Some View {
2326
VStack {
@@ -28,6 +31,26 @@ var body: Some View {
2831
2932
```
3033

34+
### NumericTextModifier
35+
36+
```
37+
// Inside your view
38+
@State private var int: NSNumber?
39+
@State private var intString = ""
40+
@State private var double: NSNumber?
41+
@State private var doubleString = ""
42+
43+
var body: Some View {
44+
VStack {
45+
TextField("Int", text: $intString)
46+
.numericText(text: $intString, number: $int, isDecimalAllowed: false)
47+
TextField("Double", text: $doubleString)
48+
.numericText(text: $doubleString, number: $double, isDecimalAllowed: true)
49+
}
50+
}
51+
52+
```
53+
3154
## Installation
3255

3356
Use Swift Package Manager or just drag and drop the two source files into your project. It supports iOS 14, macOS 10.16/11, tvOS 14, watchOS 7.

0 commit comments

Comments
 (0)