baseView.phoneField.rx.controlEvent([.editingChanged])
.asObservable()
.subscribe(onNext: { [weak self] _ in
guard let `self` = self else { return }
// 获取非选中状态文字范围
let selectedRange = self.baseView.phoneField.markedTextRange
// 没有非选中文字,截取多出的文字
if selectedRange == nil {
let text = self.baseView.phoneField.text ?? ""
if text.count > 12 {
let index = text.index(text.startIndex, offsetBy: 13)
self.baseView.phoneField.text = String(text[..<index])
}
}
})
.disposed(by: disposeBag)
网友评论