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()
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· Windows 提权-UAC 绕过