zoukankan      html  css  js  c++  java
  • SwiftUI 中自定义输入框样式

    效果图

    import SwiftUI
    
    struct ContentView: View {
        @State private var username:String = ""
        var body: some View {
            VStack {
                Text("输出:(username)")
                TextField("username", text: $username)
    //                .textContentType(.emailAddress)
    //                .keyboardType(.emailAddress)//键盘样式
    //                .autocapitalization(.none)//自动大写
    //                .disableAutocorrection(true)//不自动提示
    //                .accentColor(Color.red)//光标颜色
    //                .border(Color.gray)
                    .padding()
                    .textFieldStyle(myTextFieldStyle())
                    .textFieldStyle(UnderlineTextFieldStyle())
                    .textFieldStyle(TextFieldCleanButtonStyle(text: $username))
            }
            
        }
    }
    
    struct TextFieldCleanButtonStyle: 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(Color.gray)
                    }
                    .padding(.trailing , 10)
    
                }
            }
    //        .overlay(
    //            RoundedRectangle(cornerRadius: 12)
    //                .stroke(Color.gray,lineWidth: 1)
    //        )
    //        .padding(.horizontal , 10)
        }
    }
    
    struct myTextFieldStyle : TextFieldStyle{
        func _body(configuration:TextField<Self._Label>)->some View{
            HStack{
                Image(systemName: "magnifyingglass")
                    .foregroundColor(Color.gray)
                configuration
    //                .padding(.vertical,10)
            }
            .padding(.horizontal,10)
            .background(Color.yellow)
            .cornerRadius(20)
            .shadow(color: .gray, radius: 2)
        }
    }
    
    
    struct UnderlineTextFieldStyle: TextFieldStyle{
        func _body(configuration:TextField<Self._Label>)->some View{
            configuration
                .overlay(
                    Rectangle()
                    .frame(height:1).padding(.top ,35)
                )
                .foregroundColor(Color.pink)
    //            .padding(10)
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    
  • 相关阅读:
    eclipse下jsp文件报错解决方法
    使用vscode搭建本地的websocket
    tomcat的首次登录配置
    tomcat配置报错解决方法 The jre_home environment variable is not defined correctly
    cento升级openssl依旧显示老版本
    Centos6安装mysql5.7最新版
    Neutron服务组件
    网络OSI 7层模型
    Kubernetes的核心技术概念和API对象
    Xen 虚拟化技术
  • 原文地址:https://www.cnblogs.com/chaostudy/p/15033133.html
Copyright © 2011-2022 走看看