实现代理方法
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
}
}
网友评论