我正在使用 shouldChangeCharactersInRange 作为使用动态类型搜索的方式。
但是我遇到问题, shouldChangeCharactersInRange 在文本字段实际更新之前被调用:
在Objective C中,我使用以下方法解决了这个问题:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
return YES;
}
但是,我试过在Swift中写这个:
func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
let txtAfterUpdate:NSString = self.projectSearchTxtFld.text as NSString
txtAfterUpdate.stringByReplacingCharactersInRange(range, withString: string)
self.callMyMethod(txtAfterUpdate)
return true
}
在获取值之前,仍会调用该方法吗?
答案 0 :(得分:90)
Swift 4,Swift 5
此方法不使用NSString
// MARK: - UITextFieldDelegate
extension MyViewController: UITextFieldDelegate {
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
if let text = textField.text,
let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange,
with: string)
myvalidator(text: updatedText)
}
return true
}
}
请注意。使用安全文本字段时要小心。
答案 1 :(得分:68)
stringByReplacingCharactersInRange
返回一个新字符串,那么如何:
func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
if let text = textField.text as NSString? {
let txtAfterUpdate = text.replacingCharacters(in: range, with: string)
self.callMyMethod(txtAfterUpdate)
}
return true
}
答案 2 :(得分:40)
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let textFieldText: NSString = (textField.text ?? "") as NSString
let txtAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)
callMyMethod(txtAfterUpdate)
return true
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
callMyMethod("")
return true
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let textFieldText: NSString = textField.text ?? ""
let txtAfterUpdate = textFieldText.stringByReplacingCharactersInRange(range, withString: string)
callMyMethod(txtAfterUpdate)
return true
}
func textFieldShouldClear(textField: UITextField) -> Bool {
callMyMethod("")
return true
}
虽然textField.text
属性是可选的,但不能设置为nil。将其设置为nil将更改为UITextField
内的空字符串。在上面的代码中,如果textFieldText
为nil,则textField.text
设置为空字符串(通过nil合并运算符??
)。
实施textFieldShouldClear(_:)
处理文本字段的清除按钮可见并点按的情况。
答案 3 :(得分:10)
在 Swift 3 中,它看起来像这样:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let text: NSString = (textField.text ?? "") as NSString
let resultString = text.replacingCharacters(in: range, with: string)
return true
}
答案 4 :(得分:0)
如果您想预处理用户输入或粘贴的字符,以下解决方案就像魅力一样
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let strippedString = <change replacements string so it fits your requirement - strip, trim, etc>
// replace current content with stripped content
if let replaceStart = textField.position(from: textField.beginningOfDocument, offset: range.location),
let replaceEnd = textField.position(from: replaceStart, offset: range.length),
let textRange = textField.textRange(from: replaceStart, to: replaceEnd) {
textField.replace(textRange, withText: strippedString)
}
return false
}
在此处找到它:https://gist.github.com/Blackjacx/2198d86442ec9b9b05c0801f4e392047
答案 5 :(得分:0)
进行更改但未更新UI并等待您选择时调用此函数
看看返回的布尔值
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
true
-这意味着iOS接受更改(文本,插入符号...)false
-这意味着您应对所有这些事情负责答案 6 :(得分:-5)
要在Swift 3.0中获取我的UITextField组件中的确切文本,我使用了:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let enteredTxt = textField.text! + string
doSomethingWithTxt(enteredTxt) //some custom method
}