[SwiftUI] Create a TextField that looks like a Qiita tag

The TextField in SwiftUI isn't very customizable, but I tried to make it look like it. ezgif-3-929af5259172.gif

Implementation

struct TestView: View {
    @State private var text: String = ""
    var body: some View {
        ZStack{
            TextField("input", text: $text)
                .foregroundColor(.clear)
            HStack(spacing: 0){
                let strings = text.split(separator: " ", omittingEmptySubsequences: false)
                ForEach(strings.indices, id: \.self){i in
                    Text(strings[i])
                        .background(
                            Color(.sRGB, red: 0.847, green: 0.917, blue: 0.992)
                                .cornerRadius(5)
                                .overlay(
                                    RoundedRectangle(cornerRadius: 5)
                                        .stroke(Color(.sRGB, red: 0.745, green: 0.866, blue: 0.988))
                                )
                        )
                        .padding(.horizontal, 2)
                }
                Spacer()
            }
        }
    }
}

point

TextField is too restrictive, so make the color clear to make it invisible and display it as you like.

Constraint

The cursor disappears because the text is covered from above. To prevent this, for example, the background color of the text should be transparent, but I gave up because it was difficult to match the colors.

Recommended Posts

[SwiftUI] Create a TextField that looks like a Qiita tag
Create a List that holds multiple classes
[Java] Create something like a product search API
Let's create a custom tab view in SwiftUI 2.0
How to create a class that inherits class information