Well you could use a hacky way for this.
- First we will hold a count of the current characters the text string has.
- Whenever the user presses backspace we check in the onChange handler if the previous character count is higher than the new character count
- if that is the case we delete the whole string, or whatever you want to do when the delete button is pressed.
import SwiftUI
struct SquareView: View {
var placeholder = "test"
@State var text = "test"
@State var textLen = 4
var body: some View {
VStack {
TextField(self.placeholder, text: self.$text)
.onChange(of: self.text, perform: { value in
if value.count < textLen {
self.text = "" // << removed the whole text but here you can insert anything you want to do when the delete button is pressed
}
textLen = value.count
})
}
}
}
Keep in mind that this is a hacky way and brings risks. For example if users paste something which is shorter than the current text.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…