I want a ‘+’ Sign in the Textfield which cannot be erased. A user should be able to enter values after it and if he presses backspace it should only erase the v
In SwiftUI, you may want to use Combine framework to do this. Create an ObservableObject to handle value changes. Example code here:
import SwiftUI
import Combine
struct ContentView: View {
class ViewModel: ObservableObject {
@Published var text = "" {
didSet {
if text.prefix(1) != "+" {
text = "+" + text
}
}
}
}
@ObservedObject var viewModel = ViewModel()
var body: some View {
TextField("Placeholder", text:$viewModel.text)
}
}