简体   繁体   中英

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?

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM