我像这样更改了 TextField 样式:
TextField("Test", text: $name).textFieldStyle(CustomTextFieldStyle())
Run Code Online (Sandbox Code Playgroud)
现在我希望它在用户点击它时改变样式。
我的 CustomTextFieldStyle 定义为:
public struct CustomTextFieldStyle : TextFieldStyle {
public func _body(configuration: TextField<Self._Label>) -> some View {
configuration
.font(.callout)
.padding(10)
.background(
RoundedRectangle(cornerRadius: 4)
.strokeBorder(SBGreen, lineWidth: 2))
}
}
Run Code Online (Sandbox Code Playgroud) 我试图填充胶囊式的描边边框,但不幸的是它填充了矩形。见下图:
有什么想法可以让它只填充到笔画边框吗?我想使用该字段作为搜索框,这就是为什么我想在其中使用文本字段。
这是我的代码:
struct ContentView: View {
var body: some View {
HStack{
TextField("Search", text: .constant(""))
}
.padding()
.frame(maxWidth:300)
.background(
Capsule()
.strokeBorder(Color.black,lineWidth: 0.8)
.background(Color.blue)
.clipped()
)
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢!