如何在Swift 3 iOS中限制TextField的双/单空间

问题描述 投票:1回答:8

我的iOS应用程序中有登录页面,它是由Swift语言开发的。因为第一个字母不应该是单/双空格,并且在用户输入文本后它应该允许单个空格,不允许双倍空格并且需要设置文本长度的限制。

我想在开始输入文本时点击单/双空格时限制用户,并且在输入文本字段中的第一个字母后需要允许单个空格

我可以做单/双,但不能按照我的要求工作。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    //for double space restrict
    if (range.location > 0 && (textField.text?.characters.count)! > 0) {
        //Manually replace the space with your own space, programmatically
        //Make sure you update the text caret to reflect the programmatic change to the text view
        //Tell Cocoa not to insert its space, because you've just inserted your own
        return false
    }else {
        if textField.tag == 1 || textField.tag == 2 { //restrict text length
            guard let text = textField.text else { return true }
            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= limitLengthForTextField
        }
    }

    return true 
}

谁能提出建议。谢谢

ios swift xcode
8个回答
1
投票

试试此代码:

//TextField Validation
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
    {
        if(textField == UserNameText)
        {
            if range.location == 0 && string == " "
            {
                return false
            }
            return true
        }
        else if(textField == PasswordText)
        {
            if range.location == 0 && string == " "
            {
                return false
            }
            return true
        }
        else
        {
            return true
        }
    }

它不允许用户在第一个字母给出空间。例如,如果您需要为username字段设置限制,其限制为10个字符表示使用此代码:

//TextField Validation
        func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
        {
            if(textField == UserNameText)
            {
                if range.location == 0 && string == " "
                {
                    return false
                }
                else if range.length + range.location > (self. UserNameText.text?.characters.count)!
                {
                     return false
                }
                let NewLength = (self.let NewLength = (self.userEmailTxt.text?.characters.count)! - range.length
                return NewLength <= 9
            }
            else if(textField == PasswordText)
            {
                if range.location == 0 && string == " "
                {
                    return false
                }
                return true
            }
            else
            {
                return true
            }
        }

1
投票

试试这个

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let result = (textField.text as NSString?)?.replacingCharacters(in: range, with: string) ?? string
    let text = result as String
    if (text.range(of: "^(?i)(?![ ])(?!.*[ ]{2})[A-Z. ]*$", options: .regularExpression) != nil) {

        return true
    }
    return false
}

如果你需要限制总长度,请将正则表达式更新为^(?i)(?![ ])(?!.*[ ]{2})[A-Z. ]{1,10}$以获取10字符。根据需要更新长度。


0
投票

请检查此代码。

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

    let trimmedString = textField.text?.trimmingCharacters(in: .whitespaces)
    if (trimmedString.length == 0)
        return false

    //for double space restrict
    if (range.location > 0 && (textField.text?.characters.count)! > 0) {
        //Manually replace the space with your own space, programmatically
        //Make sure you update the text caret to reflect the programmatic change to the text view
        //Tell Cocoa not to insert its space, because you've just inserted your own
        if string.range(of:" ") != nil {
            return false
        }
        return true
    }else {
        if textField.tag == 1 || textField.tag == 2 { //restrict text length
            guard let text = textField.text else { return true }
            let newLength = text.characters.count + string.characters.count - range.length
            return newLength <= limitLengthForTextField
        }
    }

    return true
}

你的限制textfield length的代码应该正常工作,因为textfields标签为1和2。


0
投票

有用 !

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
       if (string == " " || string == "  ") &&  range.location == 0
       {
           return false
       }

       return true
}

0
投票
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let rawString = string
     let range = rawString.rangeOfCharacter(from: .whitespaces)
    if ((textField.text?.characters.count)! == 0 && range  != nil)
    || ((textField.text?.characters.count)! > 0 && textField.text?.characters.last  == " " && range != nil)  {
        return false
    }
return true
}

0
投票
func textField(_ textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if range.location == 0 && string == " " {
       return false
   } else if range.location > 1 && textField.text?.characters.last == " " && string == " " {
       return false
   }else{
       return true
   }
}

嗨,Anli请试试这个会解决你的问题。


0
投票

Swift 4.1

只需简单的单行代码就可以停止空格

对于所有Textfield

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

        textField.text = textField.text?.replacingOccurrences(of: " ", with: "")
        return true
    }

对于单文本字段

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

    if textField == txtid
    {
        textField.text = textField.text?.replacingOccurrences(of: " ", with: "")
    }
    return true
}

0
投票

我终于找到了我的要求

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

    //for double space restrict
    let str = textField.text
    if str?.characters.count == 0 && string == " " {
        return false
    } else if (str?.characters.count)! > 0 {
        if str?.characters.last == " " && string == " " {
            return false
        } else {
            if textField.tag == 1 || textField.tag == 2 { //restrict text length
                guard let text = textField.text else { return true }
                let newLength = text.characters.count + string.characters.count - range.length
                return newLength <= limitLengthForTextField
            }
        }
    }
    return true 
}
© www.soinside.com 2019 - 2024. All rights reserved.