example
if focus in textfield will display text (number normal)
if not focus textfield will display x in 3 fist digit
e.g.
You need to associate a FocusState
with the field and then respond to changes in focus. See the answer to How to detect when a TextField loses the focus in SwiftUI for iOS? for more about FocusState
.
Here is an example of how it could work:
struct ContentView: View {
@FocusState private var isFocused: Bool
@State private var masterText = ""
@State private var visibleText = ""
var body: some View {
Form {
Section {
TextField("Secret number", text: $visibleText)
.focused($isFocused)
.onChange(of: isFocused) { isFocused in
if isFocused {
visibleText = masterText
} else {
masterText = visibleText
if !visibleText.isEmpty {
let endRange = min(visibleText.count, 3)
let endOfString = visibleText.dropFirst(endRange)
visibleText = "xxx" + endOfString
}
}
}
}
Section {
TextField("Another field", text: .constant(""))
}
}
}
}