Lightweight implementation of a text field that filters and manages decimal input. It only allows integer or floating point numbers and a minus by default. It also includes few other behaviours that are often needed:
- trimming zeros in a decimal number when editing ends
- resetting the input to zero when editing ends and the field is empty
- clearing the field when editing begins and the text is zero
These are "always on" by default.
Clearing zero when editing begins can be disabled by turning off the isClearingZeroWhenEditingBegins
flag.
To disable trimming zeros in a floating point number and ensuring the text is not empty when editing ends, turn off the trimsAndSetsNonEmptyWhenEditingEnds
flag.
Other behaviour will also become customizable in the future.
To use DecimalField
in an iOS target (given it has been added to this target), it needs to be imported like this:
import DecimalField
Then it can be added and used as a usual text field, programmatically:
let decimalField = DecimalField()
view.addSubview(decimalField)
or from .xib
/.storyboard
file:
@IBOutlet weak var decimalField: DecimalField!
The implementation of DecimalField
does not rely on its delegate
, so feel free to assign and use one if necessary.
By default, only integer or decimal numbers and a minus are allowed as input. If you don't want to allow minus, set allowsNegativeNumbers
to false
. Filtering applies to input from both UI and code.
Swift Package Manager is a preferred way to connect DecimalField
to your Xcode project.
Setting exact version dependency rule is advised to prevent pulling in unwanted changes. You can find versions in the Releases section.
To make it even safer, please feel free to fork the repository.
This repository is provided under the MIT licence (see LICENSE).
Want to contribute? Feel free to create an issue and submit a pull request (you might need to fork the repository if you were not added as a collaborator).