NumericTextEntry 是一个功能强大、可扩展的库,旨在满足您的数字文本输入需求。
可用的 NumberDisplayer 组件
UIFittedFlatNumberDisplayer | UIFloatingDecimalNumberDisplayer | UIFlatNumberDisplayer |
---|---|---|
![]() |
![]() |
![]() |
特性
安装
-
安装 CocoaPods
-
将此仓库添加到您的
Podfile
target 'Example' do # IMPORTANT: Make sure use_frameworks! is included at the top of the file use_frameworks! pod 'NumericalTextEntry' end
-
从终端中的 Podfile 目录运行
pod install
-
打开 CocoaPods 创建的
.xcworkspace
文件 -
完成!
如何使用它?
NumericalTextEntry 旨在非常容易扩展。尽管它具有可扩展性,但使用 NumericalTextEntry 非常简单。
基础
要创建一个数字文本输入字段:let numberField = UINumberEntryField()
获取数字字段中的不同值
let userInputValue = numberField.rawValue
let numericValue = numberField.doubleValue
let displayedValue = numberField.displayedStringValue
定制
以下值可以定制 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)
想禁止浮点值?只需注入一个不允许浮点的 NumberFormatter!
let numberFormatter = NumberFormatter()
numberFormatter.allowsFloats = false
numberField.numberFormatter = numberFormatter
自定义数字显示器
想创建自己的 NumberDisplayer?遵循这个简单的协议!
/// 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]
}
组件图
示例
要运行示例项目,首先从 repo 克隆并从 Example 目录运行 pod install
。
要求
iOS 10.0 是必需的。
文档
阅读文档
作者
Ya Boi
许可
NumericalTextEntry遵循MIT许可证。有关更多信息,请参阅LICENSE文件。