ios shouldChangeCharactersInRange 如何在 Swift 中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25621496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How shouldChangeCharactersInRange works in Swift?
提问by Ryan
I'm using shouldChangeCharactersInRangeas a way of using on-the-fly type search.
我使用shouldChangeCharactersInRange作为使用动态类型搜索的一种方式。
However I'm having a problem, shouldChangeCharactersInRangegets called before the text field actually updates:
但是我遇到了一个问题,shouldChangeCharactersInRange在文本字段实际更新之前被调用:
In Objective C, I solved this using using below:
在目标 C 中,我使用以下方法解决了这个问题:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
return YES;
}
However, I've tried writing this in Swift:
但是,我试过用 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
}
The method still gets called before I get a value?
在我获得值之前该方法仍然被调用?
回答by Vyacheslav
Swift 4, Swift 5
斯威夫特 4、斯威夫特 5
This method doesn't use NSString
这个方法不使用 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
}
}
Note. Be careful when you use a secured text field.
笔记。使用安全文本字段时要小心。
回答by Mike Pollard
stringByReplacingCharactersInRange
return a new string, so how about:
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
}
回答by Mobile Dan
Swift 3 & 4
斯威夫特 3 & 4
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
}
Swift 2.2
斯威夫特 2.2
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
}
Though the textField.text
property is an optional, it cannot be set to nil. Setting it to nil is changed to empty string within UITextField
. In the code above, that is why textFieldText
is set to empty string if textField.text
is nil (via the nil coalescing operator ??
).
虽然该textField.text
属性是可选的,但不能设置为 nil。将其设置为 nil 将更改为UITextField
. 在上面的代码中,这就是为什么textFieldText
设置为空字符串 if textField.text
is nil (通过 nil 合并运算符??
)。
Implementing textFieldShouldClear(_:)
handles the case where the text field's clear button is visible and tapped.
实现textFieldShouldClear(_:)
处理文本字段的清除按钮可见并被点击的情况。
回答by Andrey Gordeev
In Swift 3it would look like this:
在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
}
回答by blackjacx
Swift 3
斯威夫特 3
If you want to pre-process the characters the user typed or pasted, the following solution workes like a charm
如果您想预处理用户输入或粘贴的字符,以下解决方案就像一个魅力
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
}
Find it here: https://gist.github.com/Blackjacx/2198d86442ec9b9b05c0801f4e392047
在这里找到:https: //gist.github.com/Blackjacx/2198d86442ec9b9b05c0801f4e392047
回答by Pavle Mijatovic
To get the exact text in the my UITextField component in Swift 3.0 I used:
要在 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
}