应该如何在Swift中更改字符?范围?
我正在使用shouldChangeCharactersInRange作为使用即时typessearch的一种方式。
不过,我有一个问题,在文本字段实际更新之前调用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 }
在获取值之前,该方法仍然被调用?
stringByReplacingCharactersInRange
返回一个新的string,那怎么样:
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 }
Swift 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
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
更改为空string。 在上面的代码中,这就是为什么textFieldText
设置为空string,如果textField.text
是零(通过零合并运算符??
)。
实现textFieldShouldClear(_:)
处理文本字段的清除button可见并点击的情况。
在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 }
Swift 3
如果要预先处理用户键入或粘贴的字符,以下解决scheme就像魅力一样
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它: https : //gist.github.com/Blackjacx/2198d86442ec9b9b05c0801f4e392047
为了得到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 }