RxSwift限制textfield长度

2017-10-16  本文已影响129人  Fisland_枫

限制20字符,代码如下

        let accountvalid = usernameTextField.rx.text.orEmpty.map { (text) -> Bool in
            text.characters.count > 20
            }.shareReplay(1)
        
        _ = accountvalid.subscribe(onNext: { (valid) in
            
            if valid{
                let index = self.usernameTextField.text!.index(self.usernameTextField!.text!.startIndex, offsetBy:20)
                
                self.usernameTextField.text = self.usernameTextField!.text!.substring(to: index)
            }
        })
上一篇下一篇

猜你喜欢

热点阅读