Detect backspace Event in UITextField

后端 未结 11 567
自闭症患者
自闭症患者 2020-12-04 16:35

I am searching for solutions on how to capture a backspace event, most Stack Overflow answers are in Objective-C but I need on Swift language.

First I have set deleg

相关标签:
11条回答
  • 2020-12-04 17:23

    Swift 5.3

    In some version its changed and now it says:

    When the user deletes one or more characters, the replacement string is empty.

    So answer for this:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
      if string.isEmpty {
        // do something
      }
      return true
    }
    

    If you want to detect that some characters will be deleted

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
      if range.length > 0 {
        // We convert string to NSString instead of NSRange to Range<Int>
        // because NSRange and NSString not counts emoji as one character
        let replacedCharacters = (string as NSString).substring(with: range)
      }
      return true
    }
    

    If you want detect backspaces even on empty textField

    class TextField: UITextField {
      var backspaceCalled: (()->())?
      override func deleteBackward() {
        super.deleteBackward()
        backspaceCalled?()
      }
    }
    

    Old answer

    Please don't trash your code. Just put this extension somewhere in your code.

    extension String {
      var isBackspace: Bool {
        let char = self.cString(using: String.Encoding.utf8)!
        return strcmp(char, "\\b") == -92
      }
    }
    

    And then just use it in your functions

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
      if string.isBackspace {
        // do something
      }
      return true
    }
    
    0 讨论(0)
  • 2020-12-04 17:25

    Swift 4: If the user presses the backspace button, string is empty so this approach forces textField to only accept characters from a specified character set (in this case utf8 characters) and backspaces (string.isEmpty case).

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string.cString(using: String.Encoding.utf8) != nil {
            return true
        } else if string.isEmpty {
            return true
        } else {
            return false
        }
    }
    
    0 讨论(0)
  • 2020-12-04 17:27

    In Swift 3

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        let  char = string.cString(using: String.Encoding.utf8)!
        let isBackSpace = strcmp(char, "\\b")
    
        if (isBackSpace == -92) {
             print("Backspace was pressed")
        }
        return true
    }
    

    :)

    0 讨论(0)
  • 2020-12-04 17:27

    Try this

    public func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
    if(string == "") {
    
            print("Backspace pressed");
            return true;
        }
    }
    

    Note: You can return "true" if you want to allow backspace. Else you can return "false".

    0 讨论(0)
  • 2020-12-04 17:30

    Swift 4

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        //MARK:- If Delete button click
        let  char = string.cString(using: String.Encoding.utf8)!
        let isBackSpace = strcmp(char, "\\b")
    
        if (isBackSpace == -92) {
            print("Backspace was pressed")
    
            return true
        }
    }
    
    0 讨论(0)
提交回复
热议问题