SwiftUI3.0 TextField的使用相当于UITextField

//
//  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()
    }
}

posted @   johnson--save  阅读(69)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· Windows 提权-UAC 绕过
点击右上角即可分享
微信分享提示