如何防止用户在 UITextfield - swift 中输入 '#' 和空 space(' ')?

How to prevent users from entering '#' and empty space(' ') in UITextfield - swift?

我试图阻止用户输入字符“#”和空格 (“”)。我已经了解了以下内容(请参阅下面的代码)但不确定如何完成其​​余部分...

 class CreateTags: UIViewController, UITextFieldDelegate {

 /*** OUTLETS ***/
 @IBOutlet weak var inputTxtOutlet: UITextField!


 override func viewDidLoad() {
      self.inputTxtOutlet.delegate = self
 }

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

 }

这是您的解决方案:-

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

    let characters = ["#", "$", "!", "&","@"]
    for character in characters{
        if string == character{
            print("This characters are not allowed")
            return false
        }
    }
}

使用这个 How to restrict certain characters in UITextField in Swift? 示例

您需要实施以下委托方法(因为您已经实施)

设置委托

yourtextField.delegate = self

委托方法

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

    if string == "#" || string == " " {
        return false //disallow # and a space
    }
    return true

 }

In another way you can do it as follow

创建一个包含不允许的字符的常量。

 let disallowedChars = ["#", " "] //add more chars as per your need

在委托方法中检查那些字符...

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

      if disallowedChars.contains(string) {
        return false
      }

      return true
}

这是一个单行。 (适用于复制粘贴!)

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    return !string.contains(where: {[=10=] == "#" || [=10=] == " "})
}

如果您有很多限制字符,

let restrictedCharacters: [Character] = [" ", "#", "?"]

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
   return !(Set(string).intersection(Set(restrictedCharacters)).count > 0)
}

尝试针对 UITextfields

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

 if textField == inputTxtOutlet  {
       let myCharSet = CharacterSet(charactersIn:" #")
       let output: String = string.trimmingCharacters(in: myCharSet.inverted)
       let isValid: Bool = (string == output)

        if isValid {
             return false
        } else {
             return true
                }
            }
    return true
}
extension String {

   var containsValidCharacter: Bool {
   guard self != "" else { return true }
   let hexSet = CharacterSet(charactersIn: "1234567890ABCDEFabcdef")
   let newSet = CharacterSet(charactersIn: self)
   return hexSet.isSuperset(of: newSet)

 }
}

您可以像使用 UITextFieldDelegate 一样使用它。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return (string.containsValidCharacter)
}
let restrictedChars = ["#", "*", " "]

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
   return  restrictedChars.contains(string) ? false : true 
}

将您的 "shouldChangeCharactersIn" 方法替换为以下代码

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

    let characters = ["#", " "]
    for character in characters{
        if string == character{
            print("This characters are not allowed")
            return false
        }
    }
    return true
}