说到限制输入框的字数限制,你们都会想到在textFieldDelegate的代理方法中进行判断。那若是是用RxSwift,该怎么实现呢?咱们直接来看代码:bash
let countValid = priceField.rx.text.orEmpty.map { text -> Bool in
text.count > 10
}.share(replay: 1)
countValid.subscribe(onNext: { valid in
if valid {
let index = self.priceField.text!.index(self.priceField.text!.startIndex, offsetBy: 10)
self.priceField.text = String(self.priceField.text![..<index])
}
}).disposed(by: bag)
复制代码
怎么样,是否是简单多了?ui