benpomeroy9
benpomeroy9

Reputation: 417

Automatically trigger shift button after space in TextField - SwiftUI

I want the user to be typing into a text field but it always capitalises the start of each word. I have tried the .capitalise modifier on the string but it removes any capitals in the middle of the name, so isn't what I want.

My aim is to have the keyboard automatically press SHIFT when there is a space, the user can then choose to 'un shift' or use the capitals. Is there a modifier for the textField I don't know about?

Upvotes: 1

Views: 290

Answers (1)

Harshil Patel
Harshil Patel

Reputation: 1474

Use autocapitalization():

struct ContentView: View {
    @State private var string: String = ""
    var body: some View {
        VStack(spacing: 10) {
            TextField("Enter here...", text: $string)
                .autocapitalization(.words) // <~ HERE
            Text("You wrote: \(string)")
                .foregroundColor(.blue)
        }
    }
}

Result:

enter image description here

Upvotes: 1

Related Questions