Typing always (force) lowercase or uppercase - ios swift

前端 未结 2 1364
失恋的感觉
失恋的感觉 2020-12-20 02:32

How to force to type always lowercase or uppercase for some textfields in swift language?

相关标签:
2条回答
  • 2020-12-20 02:38

    The simplest solution is to set the text field's autocapitalizationType property to .None to default to lowercase input or .AllCharacters to default to uppercase input. The user can still use the shift key to change capitalization though.

    0 讨论(0)
  • 2020-12-20 03:03

    You should return false in if block because you already updated the textfield.

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
      if textField.isEqual(textFieldBodrum) {
         textFieldBodrum.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string.lowercaseString)
         return false
      } else if textField.isEqual(textFieldYalikavak) {
                    textFieldYalikavak.text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string.uppercaseString)
        return false
      }
       return true
    }
    

    If you don't want to affect other textfields you should return true end of the shouldChangeCharactersInRange function.

    https://gist.github.com/fatihyildizhan/ac5f476aebd306b0580a1fa069f153a3

    0 讨论(0)
提交回复
热议问题