python
//Maximum number of characters for TextField
textField.rx.text
.map { text in
if let text = text, text.count > 4 {
return String(text.prefix(4))
} else {
return text ?? ""
}
}
.bind(to: textField.rx.text) //If you enter the same number that has already been entered, the next event will not flow and you will not loop infinitely.
.disposed(by: disposeBag)
――I haven't tried it, but in the case of Japanese input, this may not work, so it seems that you need to consider it separately.
--You can also use textField.rx.controlEvent (.editingChanged)
etc.
RxSwift/RxCocoa: prevent UITextField from having more than … characters [iOS] Set the maximum number of characters in UITextField [Swift] How to set a character limit for UITextField
Recommended Posts