【问题标题】:How to get range of textField text till 6 characters?如何获取 textField 文本的范围直到 6 个字符?
【发布时间】:2017-05-06 10:53:34
【问题描述】:

我有一个 UITextField 用于输入密码。
我的要求是当 textField 字符范围达到第 6 个字符时,我必须进行服务器验证,并且必须将数据放入标签中。如果字符范围小于 5,那么我将传递 nil 值。

下面是我的代码:-

//TextField character range
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if textField == pinCode
    {
        guard let text = textField.text else { return  true }

        let newLength = text.characters.count + string.characters.count - range.length
        if(5 >= newLength)
        {
            print(newLength)
        }

        else
        {
            if newLength == 6
            {
                serverValidation()

                print(textField.text!)
                setFields(city: "Ajmer", state: "Rajasthan", country: "India")
                return newLength <= 6
            }
            else
            {
                 return newLength <= 6
            }
        }

    }

    return true
} 

我的问题是当 (newLength == 6) 时,我得到的 textField 文本只有 5 个字符。我的意思是,如果假设邮政编码是 560068,那么我只会收到 56006 的文本。 (但我必须在这种情况下对 6 个字符的邮政编码进行服务器验证)

所以如果 (newLength == 6) 那么我怎样才能在文本字段中获得 6 个字符,然后我只能在这里进行服务器验证。

我是 Swift 的新手。

谢谢

【问题讨论】:

  • 这是在将新字符添加到文本字段的字符串之前,如果我没记错的话,您的全文应该是textField.text+string
  • 好的,让我检查一下。我会让你知道。谢谢

标签: ios swift uitextfield character string-length


【解决方案1】:

shouldChangeCharactersIn 将在获取UITextField 中的实际文本之前被调用

这是您从 UITextField 获取全文的方式

let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)

编辑

这就是你可以使用它的方式

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    if textField == pinCode
    {
        guard let text = textField.text else { return  true }

        let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
        let newLength = newString.characters.count

        if(5 >= newLength)
        {
            print(newLength)
        }

        else
        {
            if newLength == 6
            {
                serverValidation()

                print(newString)
                setFields(city: "Ajmer", state: "Rajasthan", country: "India")
                return newLength <= 6
            }
            else
            {
                 return newLength <= 6
            }
        }

    }

    return true
} 

【讨论】:

  • 编辑了我的答案检查
  • 非常感谢 :-) 拯救我的一天。
【解决方案2】:

试试这个

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

if textField == pinCode
{
    guard let text = textField.text else { return  true }

    var txtAfterUpdate:NSString = textField.text! as NSString
    txtAfterUpdate = txtAfterUpdate.stringByReplacingCharactersInRange(range, withString: string)
    let txtString = txtAfterUpdate as String

    let newLength = txtString.characters.count
    if(5 >= newLength)
    {
        print(newLength)
    }

    else
    {
        if newLength == 6
        {
            print(textField.text!)
            setFields(city: "Ajmer", state: "Rajasthan", country: "India")
            return newLength <= 6
        }
        else
        {
            return newLength <= 6
        }
    }

}

return true
}

【讨论】:

  • 我试过了。它给出了相同的结果。我的意思是 5 个字符,仅当 (newLength == 6) 谢谢
【解决方案3】:

为此使用 TextField 委托方法。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

 let currentCharacterCount = textField.text?.characters.count ?? 0
            if (range.length + range.location > currentCharacterCount+1)
            {
                return false
            }

            print(currentCharacterCount)
            let newLength = currentCharacterCount + string.characters.count - range.length
            return newLength <= 6 //this will not allow you to enter more than 6 characters 

}

在那次通话之后

func textFieldDidEndEditing(_ textField: UITextField)
 {
             print(textField.text!)
            setFields(city: "Ajmer", state: "Rajasthan", country: "India")
}

要调用此方法,您可以使用view.endEditing(true) 方法。

【讨论】:

    【解决方案4】:

    你应该从textfield.text.length + string.length 得到newlength

    然后从textfield.text + string获取文本。

    在调用返回 true 之前,您无法从 textfield.text 获取新文本。

    【讨论】:

    • 对于新长度,你的意思是我必须这样做:- let newLength = textField.text.length + string.length
    • 是的。有用?我在 Objective C 上工作,所以我不能把它翻译成 Swift。
    猜你喜欢
    • 2020-09-14
    • 2022-11-30
    • 1970-01-01
    • 2018-01-26
    • 2021-11-12
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多