Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a bottom line on TextField (SwiftUI)

Tags:

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

enter image description here

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 
like image 720
Shawn Baek Avatar asked Jul 13 '19 11:07

Shawn Baek


People also ask

What is prompt in TextField SwiftUI?

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.

How to use textfield for user input in Swift?

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?

How much space does a text view take in SwiftUI?

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.

How to align text view along the horizontal axis in SwiftUI?

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.

How to create a button using SwiftUI?

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 }


2 Answers

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) 

enter image description here

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()         }     } } 
like image 101
Harshal Valanda Avatar answered Sep 28 '22 09:09

Harshal Valanda


Using .overlay

TextField("[email protected]", text: $email)         .overlay(VStack{Divider().offset(x: 0, y: 15)}) 

enter image description here

the use of VStack is to ensure that the Divider is always horizontal.

like image 44
iGhost Avatar answered Sep 28 '22 10:09

iGhost