Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have trouble using cornerradius and borders on a textfield in SwiftUI

I have a text field in swiftUI, and in order to make it more appealing I'd like to add a border and have rounded corners. But it doesn't seem to work like it's supposed to (see image). What did I miss?

image 1

I've tried putting .cornerradius() before .border(), but it had the same effect.

TextField("Text input goes here", text: $addMins)
    .padding(.all, 5.0)
    .background(View)
    .frame(width: 300.0, height: 35.0)
    .border(Color.green, width: 2)
    .cornerRadius(14)
like image 840
Sydney Avatar asked Apr 16 '21 21:04

Sydney


People also ask

How do you put a border around text in SwiftUI?

Rounded border around Text view in SwiftUI A default, rectangular border can be added to a stack view using the.border () modifier: VStack { Text ("Simple Swift Guide"). font (. largeTitle) Text ("SwiftUI Tutorials"). font (. title). foregroundColor (. gray) }. padding (). border (Color. black, width: 4)

How to add a rounded border to a text view?

Add rounded border to a Text view using .overlay () modifier and RoundedRectangle shape with a custom stroke width: A default, rectangular border can be added to a stack view using the .border () modifier: Remember that in order to add a border with rounded corners to a stack view, you need to use the method with .overlay () modifier.

How to create a border with rounded corners in Xcode 11?

Before the release of Xcode 11 beta 6, you can use the border modifier and pass it with the corner radius: However, the latest beta of Xcode 11 has deprecated the function call. To create a border with rounded corners, you can draw a rounded rectangle and overlay on the button like this:

How to add border to text view in jQuery?

Create rectangular or rounded-corners borders using .border () or .overlay () modifiers respectively. Create solid line or dashed borders. Add default, rectangular border to a Text view using .border () modifier:


4 Answers

Here a simple way:


enter image description here


struct ContentView: View {
    
    @State private var stringOfTextField: String = String()

    var body: some View {
        
        TextField("Enter text . . .", text: $stringOfTextField)
            .padding()
            .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.black, style: StrokeStyle(lineWidth: 1.0)))
            .padding()

    }
}
like image 157
swiftPunk Avatar answered Sep 19 '22 06:09

swiftPunk


So you want something like this?

TextField("Text Field", text: $text)
    .padding(4)
    .overlay(
        RoundedRectangle(cornerRadius: 14)
            .stroke(Color.green, lineWidth: 2)
    )
    .padding()

textfield with green rounded border

like image 32
Patrick Wynne Avatar answered Oct 19 '22 16:10

Patrick Wynne


Here a simple way:


enter image description here


struct ContentView: View {
    
    @State private var stringOfTextField: String = String()

    var body: some View {
        
        TextField("Enter text . . .", text: $stringOfTextField)
            .padding()
            .overlay(RoundedRectangle(cornerRadius: 10.0).strokeBorder(Color.black, style: StrokeStyle(lineWidth: 1.0)))
            .padding()

    }
}
like image 15
ios coder Avatar answered Oct 19 '22 18:10

ios coder


By the way, if you need a stroked and corner radiused text field with a different background, here's a solution

TextField("Placeholder", text: $text)
    .padding()
    .background(RoundedRectangle(cornerRadius: 5).fill(Color.gray))
    .overlay(
        RoundedRectangle(cornerRadius: 5)
            .stroke(lineWidth: 1)
    )
    .foregroundColor(.black)
like image 13
Eduard Streltsov Avatar answered Oct 19 '22 16:10

Eduard Streltsov