使用 RxSwift 即时更改文本字段的格式
Change format of a textfield on the fly using RxSwift
我想为我的文本字段提供货币格式,这样当我键入时它看起来像:
0.00 美元
0.09 美元
0.98 美元
9.87 美元
$98.76
所以我所做的是:
ObservableType
的扩展,具有 2 个功能,一个用于转换为所需格式,另一个用于读取值:
extension ObservableType where E == String {
func currencyFormattedToValue() -> Observable<E> {
return asObservable().flatMap { currencyString -> Observable<E> in
let formatter = Constants.moneyFormatter
let value = Double(truncating: formatter.number(from: currencyString) ?? 0.00).rounded(toPlaces: 2)
return Observable.just("\(value)")
}
}
func valueToCurrencyFormatted() -> Observable<E> {
return asObservable().flatMap { valueString -> Observable<E> in
let formatter = Constants.moneyFormatter
var amountWithPrefix = valueString
var number: NSNumber!
let regex = try! NSRegularExpression(pattern: "[^0-9]", options: .caseInsensitive)
amountWithPrefix = regex.stringByReplacingMatches(in: amountWithPrefix, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, valueString.count), withTemplate: "")
let double = (amountWithPrefix as NSString).doubleValue
number = NSNumber(value: (double / 100))
guard number != 0 as NSNumber else {
return Observable.just("[=10=].00")
}
return Observable.just(formatter.string(from: number)!)
}
}
}
然后我像这样使用它们:
textField.rx.text
.orEmpty
.changed
.valueToCurrencyFormatted()
.bind(to: self.textField.rx.text)
.disposed(by: disposeBag)
并读取值并将其绑定到主题:
textField.rx.text
.orEmpty
.currencyFormattedToValue()
.bind(to: viewModel.amountValue)
.disposed(by: disposeBag)
我的格式化程序:
class Constants {
static var moneyFormatter:NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .currencyAccounting
formatter.currencySymbol = "$"
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
return formatter
}
}
我正在接受你的建议,谢谢!
我想为我的文本字段提供货币格式,这样当我键入时它看起来像:
0.00 美元
0.09 美元
0.98 美元
9.87 美元
$98.76
所以我所做的是:
ObservableType
的扩展,具有 2 个功能,一个用于转换为所需格式,另一个用于读取值:
extension ObservableType where E == String {
func currencyFormattedToValue() -> Observable<E> {
return asObservable().flatMap { currencyString -> Observable<E> in
let formatter = Constants.moneyFormatter
let value = Double(truncating: formatter.number(from: currencyString) ?? 0.00).rounded(toPlaces: 2)
return Observable.just("\(value)")
}
}
func valueToCurrencyFormatted() -> Observable<E> {
return asObservable().flatMap { valueString -> Observable<E> in
let formatter = Constants.moneyFormatter
var amountWithPrefix = valueString
var number: NSNumber!
let regex = try! NSRegularExpression(pattern: "[^0-9]", options: .caseInsensitive)
amountWithPrefix = regex.stringByReplacingMatches(in: amountWithPrefix, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: NSMakeRange(0, valueString.count), withTemplate: "")
let double = (amountWithPrefix as NSString).doubleValue
number = NSNumber(value: (double / 100))
guard number != 0 as NSNumber else {
return Observable.just("[=10=].00")
}
return Observable.just(formatter.string(from: number)!)
}
}
}
然后我像这样使用它们:
textField.rx.text
.orEmpty
.changed
.valueToCurrencyFormatted()
.bind(to: self.textField.rx.text)
.disposed(by: disposeBag)
并读取值并将其绑定到主题:
textField.rx.text
.orEmpty
.currencyFormattedToValue()
.bind(to: viewModel.amountValue)
.disposed(by: disposeBag)
我的格式化程序:
class Constants {
static var moneyFormatter:NumberFormatter {
let formatter = NumberFormatter()
formatter.numberStyle = .currencyAccounting
formatter.currencySymbol = "$"
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
return formatter
}
}
我正在接受你的建议,谢谢!