将文本设置为shouldChangeCharactersIn中的UITextField后,光标将结束

时间:2018-08-10 10:33:44

标签: ios swift uitextfield

我有一个带有电话号码的文字标签。我会在用户输入时屏蔽电话号码,以便在shouldChangeCharactersIn函数中使用

  • 我得到用户输入(字符串)
  • 将该输入添加到已经用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 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
}

我想要的GIF图片

enter image description here

1 个答案:

答案 0 :(得分:3)

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
    }