2

I'm working on an app where the user needs to be able to type 'forwards' but is not able to delete anything (to go back and edit for example) that has already been typed.

There are a few answers that are roughly what I want :

how to handle backspace in uitextfield - this is an old version of Swift i think as I got a lot of errors following this (deprecated, missing arguments etc). When trying this, I was thinking of doing something like the below :

func disableBackspace() {

//do a check to see if the range of characters has reduced
if range of characters in the string reduces (<0?) {

//if it has then don't allow the 'character' to be typed
return false 
}
}

So I then liked the look of this method :

want to know ever time while pressing on keyboard back button during textfield editing ios

So I tried :

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

//meant to detect if character was a backspace
let char = string.cString(using: String.Encoding.utf8)
let isBackSpace: Int = Int(strcmp(char, "\u{8}"))
if isBackSpace == -8 {
    print("Backspace was pressed")
}
//meant to return false to not allow the backspace
        return false
}

And have made my VC a textView delegate by doing

override func viewDidLoad() {
    self.textViewOutlet.delegate = self

    super.viewDidLoad()
}

but that didn't work. It doesn't print anything and still allows the user to backspace.

Any ideas much appreciated!

nc14
  • 539
  • 1
  • 8
  • 26

2 Answers2

3

You need to simply implement textView(_:shouldChangeTextIn:replacementText:) and check for empty replacementText.

Empty replacementText == backspace pressed

If the text is Empty 1return false. Elsereturn true`.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if text.isEmpty {
        return false
    }
    return true
}
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • this worked perfectly - there are quite a lot of questions and answers about how to detect backspace being pressed in Swift - this is the best answer by an absolute mile thanks – nc14 Jun 06 '19 at 11:50
  • will do - says i have to wait another 5 minutes! – nc14 Jun 06 '19 at 11:53
0

Use the following code:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    let  char = text.cString(using: String.Encoding.utf8)!
    let isBackSpace = strcmp(char, "\\b")

    if (isBackSpace == -92) {
        return false
    }
    return true
}
Bhavik Modi
  • 1,517
  • 14
  • 29
  • hi - thanks, I saw this on another answer somewhere but I didn't understand why it would equal 92 - do you know? – nc14 Jun 06 '19 at 11:53
  • 1
    strcmp() is basically C language function which returns the difference in number between two characters. Here -92 is the difference between "Backspace" and "\\b" character. – Bhavik Modi Jun 06 '19 at 11:58
  • 1
    You can refer further details here: https://stackoverflow.com/questions/51577566/swift-why-strcmp-of-backspace-returns-92 – Bhavik Modi Jun 06 '19 at 12:00
  • 1
    @nc14 I have tested and the code is working as you expected. – Bhavik Modi Jun 06 '19 at 12:18