//
// ContentView.swift
// TextFieldSupplement
//
// Created by lujun on 2021/12/19.
//
import SwiftUI
struct ContentView: View {
var body: some View {
clearButton
}
@State var email: String = ""
var clearButton: some View {
TextField("input your email",text: $email)
.autocapitalization(.none)
.textFieldStyle(TextFieldClearButtonStyle(text: $email))
}
struct TextFieldClearButtonStyle: TextFieldStyle {
@Binding var text: String
func _body(configuration: TextField<Self._Label>) -> some View {
HStack{
configuration.padding()
if !text.isEmpty {
Button {
self.text = ""
} label: {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.gray)
}
.padding(.trailing,10)
}
}
.overlay(RoundedRectangle(cornerRadius: 12)
.stroke(Color.gray,lineWidth: 1)
).padding(.horizontal,10)
}
}
//MARK: - placeHodler 颜色
/* var placeHolderColor: some View {
}
struct SuperTextField: View {
let placeHolder: Text
@Binding var text: String
let onEditingChange: (Bool) -> View
let onCommit: () -> Void
var body: some View {
ZStack(alignment: .leading) {
if text.isEmpty {
HStack {
placeHolder
Spacer()
}
}else{
TextField("",text: $text,onEditingChanged: onEditingChange,onCommit: onCommit)
}
}
.border(Color.gray)
}
}*/
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
![]()