How to restrict certain characters in UITextField in Swift?

前端 未结 8 2026
野的像风
野的像风 2020-12-06 19:44

I am creating a trivia application that asks for a username on start up. I\'d like to make it impossible to use characters such as #$@!^& etc (also including \"space\").

8条回答
  •  清歌不尽
    2020-12-06 20:33

    internal func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{
         if let text = string{
               if text == "#" || text == "$" || text == "!"{ \\and so on
                  return false
               }
         }
         return true
    }
    

提交回复
热议问题