本文介绍了在 shouldChangeCharactersIn 中将文本设置为 UITextField 后,光标将结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含电话号码的文本标签.我在用户输入时屏蔽电话号码,以便在 shouldChangeCharactersIn 函数中;

I have text label which has phone number in it. I mask the phone number when user is typing so that in shouldChangeCharactersIn function;

  • 我得到用户输入(字符串)
  • 将该输入添加到已经写入 UITextField
  • 的文本中
  • 屏蔽文本并将其设置为 UITextField
  • 返回错误

我的问题是,在我设置了 UITextfield 的文本后(删除或添加新字符 UITextField,光标移动到末尾但我希望它保持在同一位置.(意思是相同的位置,我的意思是当我没有实现 shouldChangeCharactersIn 功能时)我该怎么做?谢谢.

My question is that after I set text of UITextfield (delete or add new character UITextField, cursor moves to the end but I want it to stay in the same position. (By meaning same position, I mean same when I don't implement shouldChangeCharactersIn function) How can I do that? Thank you.

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 图像

What I want in GIF

推荐答案

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
    }

这篇关于在 shouldChangeCharactersIn 中将文本设置为 UITextField 后,光标将结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 18:01