NumericTextEntry is a powerful, extensible library intended for your numeric text entry needs.
Available NumberDisplayer components:
UIFittedFlatNumberDisplayer | UIFloatingDecimalNumberDisplayer | UIFlatNumberDisplayer |
---|---|---|
![]() |
![]() |
![]() |
Features
Installation
-
Install CocoaPods
-
Add this repo to your
Podfile
target 'Example' do # IMPORTANT: Make sure use_frameworks! is included at the top of the file use_frameworks! pod 'NumericalTextEntry' end
-
Run
pod install
in the podfile directory from your terminal -
Open up the
.xcworkspace
that CocoaPods created -
Done!
How Do I Work With It?
NumericalTextEntry is intended to be very extensible. Despite its extensibility, NumericalTextEntry is very easy to work with.
Basics
To create a numeric text entry field: let numberField = UINumberEntryField()
To get the different values in the number field:
let userInputValue = numberField.rawValue
let numericValue = numberField.doubleValue
let displayedValue = numberField.displayedStringValue
Customization
The following values are customizable for UINumberEntryFields
:
// Cap the maximum value for entry to 987.2 (default 9_999_999_999_999.99; exceeding the default value may cause formatting errors due to double-precision.)
numberField.maximumValue = 987.2
// Set the starting value for the text entry field.
numberField.startingValue = 37.2
// Determines if decimals should not be in the displayedString if the number can be represented by an integer
// e.g.: rawValue: "23" - formattedNumber: "23.00" - displayedNumber: "23")
// e.g. 2: rawValue: "23.0" - formattedNumber: "23.00" - displayedNumber: "23.00"
numberField.hideDecimalsIfIntegerValue = false
// Change the number formatter for the view
numberField.numberFormatter = userDefinedNumberFormatter
// Change the number displayer.
numberField.numberDisplayer = UIFloatingDecimalNumberDisplayer()
numberField.numberDisplayer = UIFlatNumberDisplayer()
numberField.numberDisplayer = UIFittedFlatNumberDisplayer()
// NumberDisplayer customization: both the UIFloatingDecimalNumberDisplayer and UIFittedFlatNumberDisplayer have toggleable animations.
let numberDisplayer = UIFittedFlatNumberDisplayer()
numberDisplayer.animated = false
// Custom fonts and text colors are available for every text displayer as well!
let numberDisplayer = UIFittedFlatNumberDisplay(withFont: customFont, withTextColor: UIColor.red)
Want to disallow float values? Simply inject a NumberFormatter that doesn't allow floats!
let numberFormatter = NumberFormatter()
numberFormatter.allowsFloats = false
numberField.numberFormatter = numberFormatter
Custom NumberDisplayers
Want to create your own NumberDisplayer? Follow this simple protocol!
/// A protocol used to display numbers
public protocol UINumberDisplayer {
/// Displays the provided value.
///
/// - Parameters:
/// - stringToDisplay: The complete string to display.
/// - rawValue: The raw value of the string to display (not formatted)
/// - numberFormatter: The formatter used to generate the stringToDisplay
/// - view: The view where the value should be displayed in
/// - Returns: The views that were created to hold the displayed value.
func displayValue(_ stringToDisplay: String, withRawString rawString: String,
numberFormatter: NumberFormatter, inView view: UIView) -> [UIView]
}
Component Diagram
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requirements
iOS 10.0 is required.
Documentation
Read the docs
Author
Ya Boi
License
NumericalTextEntry is available under the MIT license. See the LICENSE file for more info.