Ios 在shouldChangeCharactersIn中将文本设置为UITextField后,光标将结束
我有一个文本标签,里面有电话号码。当用户键入时,我会屏蔽电话号码,以便在Ios 在shouldChangeCharactersIn中将文本设置为UITextField后,光标将结束,ios,swift,uitextfield,Ios,Swift,Uitextfield,我有一个文本标签,里面有电话号码。当用户键入时,我会屏蔽电话号码,以便在中应更改功能中的字符 我获取用户输入(字符串) 将该输入添加到已写入UITextField 屏蔽文本并将其设置为UITextField 返回错误 我的问题是,在我设置了UITextfield(删除或添加新字符UITextfield)的文本后,光标移到了末尾,但我希望它保持在相同的位置。(意思是相同的位置,我的意思是当我没有实现shouldChangeCharactersIn函数时相同)我该怎么做?谢谢 func text
中应更改功能中的字符
- 我获取用户输入(字符串)
- 将该输入添加到已写入
UITextField
- 屏蔽文本并将其设置为
UITextField
- 返回错误
我的问题是,在我设置了UITextfield
(删除或添加新字符UITextfield
)的文本后,光标移到了末尾,但我希望它保持在相同的位置。(意思是相同的位置,我的意思是当我没有实现shouldChangeCharactersIn
函数时相同)我该怎么做?谢谢
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
return false
}
if let text = textField.text {
let newLength = text.count + string.count - range.length
let newText = text + string
let textFieldText: NSString = (textField.text ?? "") as NSString
let txtAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)
if(newLength <= 15){
//textField.text = txtAfterUpdate
textField.text = txtAfterUpdate.digits.applyPatternOnNumbers()
return false
}
return newLength <= 15
}
return true
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
保护字符集(字符sin:“0123456789”)。isSuperset(of:CharacterSet(字符sin:string))else{
返回错误
}
如果let text=textField.text{
让newLength=text.count+string.count-range.length
让newText=文本+字符串
将textFieldText:NSString=(textField.text???)设为NSString
让txtAfterUpdate=textFieldText.replacingCharacters(在:范围内,带:字符串)
if(newLengthfunc textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
保护字符集(字符sin:“0123456789”)。isSuperset(of:CharacterSet(字符sin:string))else{
返回错误
}
如果let text=textField.text{
让newLength=text.count+string.count-range.length
让newText=文本+字符串
将textFieldText:NSString=(textField.text???)设为NSString
让txtAfterUpdate=textFieldText.replacingCharacters(在:范围内,带:字符串)
如果(新长度2){
当前位置=1
}否则{
当前位置=0
}
}否则如果(当前位置==7){
当前位置=4
}否则如果(currentPosition==11){
当前位置=9
}否则如果(当前位置==14){
当前位置=12
}否则{
如果(当前位置!=1){
currentPosition=currentPosition-1
}
}
}否则{
如果(currentPosition==0){
当前位置=2
}else if(当前位置==4){
当前位置=7
}否则如果(当前位置==9){
当前位置=11
}否则如果(当前位置==12){
当前位置=14
}否则{
当前位置=当前位置+1
}
}
textField.text=txtAfterUpdate.applyPatternOnNumbers()
打印(“textField长度->:\(textField.text?.count??0)”)
如果让newPosition=textField.position(from:textField.beginingofdocument,offset:currentPosition){
textField.selectedTextRange=textField.textRange(从:newPosition到:newPosition)
}
返回错误
}
返回NealStudio @埃姆雷Unter Nead,它可以帮助你,当我使它返回真,它增加了额外的字符,因为我们已经设置了新的文本与TeXField.Trime= @埃姆雷Onter No.China我更新的代码。它将按你想要的标准工作。谢谢。这不会屏蔽电话号码吗?@埃姆雷OutnDe请再检查一次它将被完成。
func applyPatternOnNumbers(pattern: String = "(###) ### ## ##", replacmentCharacter: Character = "#") -> String {
var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
for index in 0 ..< pattern.count {
guard index < pureNumber.count else { return pureNumber }
let stringIndex = String.Index(encodedOffset: index)
let patternCharacter = pattern[stringIndex]
guard patternCharacter != replacmentCharacter else { continue }
pureNumber.insert(patternCharacter, at: stringIndex)
}
return pureNumber
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
return false
}
if let text = textField.text {
let newLength = text.count + string.count - range.length
let newText = text + string
let textFieldText: NSString = (textField.text ?? "") as NSString
let txtAfterUpdate = textFieldText.replacingCharacters(in: range, with: string)
if(newLength <= 15){
var currentPosition = 0
if let selectedRange = textField.selectedTextRange {
currentPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.start)
}
if(string == ""){
if(currentPosition == 2){
if(textField.text!.count > 2){
currentPosition = 1
}else{
currentPosition = 0
}
}else if(currentPosition == 7){
currentPosition = 4
}else if(currentPosition == 11){
currentPosition = 9
}else if(currentPosition == 14){
currentPosition = 12
}else{
if(currentPosition != 1){
currentPosition = currentPosition - 1
}
}
}else{
if(currentPosition == 0){
currentPosition = 2
}else if(currentPosition == 4){
currentPosition = 7
}else if(currentPosition == 9){
currentPosition = 11
}else if(currentPosition == 12){
currentPosition = 14
}else{
currentPosition = currentPosition + 1
}
}
textField.text = txtAfterUpdate.applyPatternOnNumbers()
print("textField Length -> : \(textField.text?.count ?? 0)")
if let newPosition = textField.position(from: textField.beginningOfDocument, offset: currentPosition) {
textField.selectedTextRange = textField.textRange(from: newPosition, to: newPosition)
}
return false
}
return newLength <= 15
}
return true
}