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)//不⾃动提⽰
// .d)//光标颜⾊
// .ay)
.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 {
< = ""
} label: {
Image(systemName: "xmark.circle.fill")
.ay)
}
.padding(.trailing , 10)
}
}
// .overlay(
/
/ RoundedRectangle(cornerRadius: 12)
// .ay,lineWidth: 1)
// )
// .padding(.horizontal , 10)
}
}
struct myTextFieldStyle : TextFieldStyle{
func _body(configuration:TextField<Self._Label>)->some View{
HStack{
Image(systemName: "magnifyingglass")
.ay)
configuration
// .padding(.vertical,10)
}
.padding(.horizontal,10)
.llow)
.cornerRadius(20)
.shadow(color: .gray, radius: 2)
}
textstyle}
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()
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论