美文网首页
swift3实时监听UITextField输入

swift3实时监听UITextField输入

作者: 码代码的魔法师 | 来源:发表于2017-10-18 14:31 被阅读0次

    实现代理方法

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
    DLog(textField.text)
    
    let tmpText = textField.text ?? ""
    
    let newText = tmpText.replacingCharacters(in: range.toRange(string: tmpText), with: string)
    
    DLog(newText.characters.count)
    
    return true
    
    }
    

    扩展NSRange

    extension NSRange {
       
            func toRange(string: String) -> Range<String.Index> {
                
                let startIndex = string.index(string.startIndex, offsetBy: self.location)
                let endIndex = string.index(startIndex, offsetBy: self.length)
                return startIndex..<endIndex
                
            }
     
    
    }
    
    

    相关文章

      网友评论

          本文标题:swift3实时监听UITextField输入

          本文链接:https://www.haomeiwen.com/subject/arxeuxtx.html