I use Rectangle()
to adding a bottom border on TextField (SwiftUI)
But I want to use protocol TextFieldStyle
for a bottom line on TextField Style like an RoundedBorderTextFieldStyle
How can I make a custom style for TextField without using Rectangle?
https://developer.apple.com/documentation/swiftui/staticmember
struct ContentView : View { @State private var username = "Text Hellow" var body: some View { VStack() { TextField($username) .foregroundColor(Color.yellow) Rectangle() .frame(height: 1.0, alignment: .bottom) .relativeWidth(1) .foregroundColor(Color.red) } .padding() } func editChanged() { } } #if DEBUG struct ContentView_Previews : PreviewProvider { static var previews: some View { ContentView() } } #endif
A prompt is the label in a text field that informs the user about the kind of content the text field expects. In a default TextField it disappears when the user starts typing, hiding this important information.
As you might know, we can use TextField for user input. All we need to do is to create a TextField and assign it to any Binding of a String value. Let’s take a look at a very quick example. In the example above, we create a TextField with a placeholder and bind it to a state variable. Pretty easy, right?
By default, a Text view in SwiftUI will take the least amount of space just to fit its content. Here is an example of a multiline text view. I set the border to pink to demonstrate the actual frame of the text view. Text("Hello, SwiftUI! Lorem ipsum dolor sit amet.") Text view only takes enough space to hold its content.
To align a text view along the horizontal axis, you need to use .frame () modifier with maxWidth set to .infinity and alignment to the alignment you want. In this example, we align our text to the traling edge with .frame (maxWidth: .infinity, alignment: .trailing). Text("Hello, SwiftUI! Lorem ipsum dolor sit amet.") Align to the traling edge.
It’s very easy to create a button using SwiftUI. Basically, you can use the code snippet below to create a button: Button(action: { // What to perform }) { // How the button looks like }
Divider
A visual element that can be used to separate other content.
You can set color
and height
Divider() .frame(height: 1) .padding(.horizontal, 30) .background(Color.red)
struct LoginField: View { @State private var email: String = "" @State private var password: String = "" var body: some View { VStack { TextField("Email", text: $email) .padding(.horizontal, 30).padding(.top, 20) Divider() .padding(.horizontal, 30) TextField("Password", text: $password) .padding(.horizontal, 30).padding(.top, 20) Divider() .padding(.horizontal, 30) Spacer() } } }
Using .overlay
TextField("[email protected]", text: $email) .overlay(VStack{Divider().offset(x: 0, y: 15)})
the use of VStack
is to ensure that the Divider
is always horizontal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With