I Have Trouble Using Cornerradius and Borders on a Textfield in Swiftui

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

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

Corner Radius of both HStack and TextField not updating

The problem is here:

.padding() /// 1.
.background(Color(.systemGray6)) /// 2.
.padding() /// 3.
.cornerRadius(12) /// 4.
  1. Your text field has a padding
  2. background gets applied, after the padding
  3. You add another padding, after the background
  4. You apply another cornerRadius on top of padding

As a result, it's the padding that gets rounded, not the background.

Diagram showing transparent padding rounded

Instead, you want to apply the cornerRadius immediately after the background.

Diagram showing background rounded, then padding on outside

struct ContentView: View {
@State var searchText = ""
var body: some View {
ZStack {
//function that's the content argument to ZStack
Color((.systemGreen))
VStack {
Text("App")
.font(.largeTitle)
.foregroundColor(Color.white)

TextField("Searchstring", text: $searchText)
.padding()
.background(Color(.systemGray6))
.cornerRadius(12) /// corner radius immediately after the background
.padding() /// extra padding outside the background
}
}
}
}

Result:

Background is rounded, then padding applied outside

MacOS swiftUI Custom ui TextField field with rounded edge

Try to use rounded rectangle in background (with needed parameters, like corner radius, color, etc) instead of border, like

.background(
RoundedRectangle(cornerRadius: 8)
.stroke(your_color_here)
)

SwiftUI - rounded textField

Add file color for RoundedRectangle.

.background(RoundedRectangle(cornerRadius: 20).fill(Color(red: 237/255, green: 237/255, blue: 237/255))) // Here!!
.foregroundColor(.black)

TextField stroke border text with SwiftUI

Here is a solution using .trim on two RoundedRectangles based on the length of the label text, which should give you the result you want:

struct CustomTextField: View {
let placeholder: String
@Binding var text: String

@State private var width = CGFloat.zero
@State private var labelWidth = CGFloat.zero

var body: some View {
TextField(placeholder, text: $text)
.foregroundColor(.gray)
.font(.system(size: 20))
.padding(EdgeInsets(top: 15, leading: 10, bottom: 15, trailing: 10))
.background {
ZStack {
RoundedRectangle(cornerRadius: 5)
.trim(from: 0, to: 0.55)
.stroke(.gray, lineWidth: 1)
RoundedRectangle(cornerRadius: 5)
.trim(from: 0.565 + (0.44 * (labelWidth / width)), to: 1)
.stroke(.gray, lineWidth: 1)
Text(placeholder)
.foregroundColor(.gray)
.overlay( GeometryReader { geo in Color.clear.onAppear { labelWidth = geo.size.width }})
.padding(2)
.font(.caption)
.frame(maxWidth: .infinity,
maxHeight: .infinity,
alignment: .topLeading)
.offset(x: 20, y: -10)

}
}
.overlay( GeometryReader { geo in Color.clear.onAppear { width = geo.size.width }})
.onChange(of: width) { _ in
print("Width: ", width)
}
.onChange(of: labelWidth) { _ in
print("labelWidth: ", labelWidth)
}


}
}

Sample Image

UITextField - Rounded corner issue

Just remove this line...

mobileNumberField.borderStyle = UITextBorderStyleRoundedRect;

and add this code also..

    [mobileNumberField setBackgroundColor:[UIColor whiteColor]];
[mobileNumberField.layer setBorderColor:[UIColor grayColor].CGColor];
[mobileNumberField.layer setBorderWidth:1.0];

Round Specific Corners of Stroke/Border SwiftUI

Here's an alternative / easier way to recreate this. Add a white background to each of the rows and add a single gray background color behind them. Add spacing between the rows to let the gray background color appear like a divider between them. Then just add a rectangle overlay to the entire view, like you already had in your code!

struct CornerView: View {
var body: some View {
VStack(spacing: 1) {
ForEach(0..<5) { index in
Text("Item \(index)")
.frame(maxWidth: .infinity)
.frame(height: 55)
.background(Color.white)
}
}
.background(Color.gray)
.overlay(
RoundedRectangle(cornerRadius: 5)
.stroke(Color.gray, lineWidth: 1)
)
.padding()
}
}

struct CornerView_Previews: PreviewProvider {
static var previews: some View {
CornerView2()
}
}

How to add with to border UITextField Swift when when create Rounded corners

One approach would be to create a custom UIView class with an embedded text field.

Here's an example, using @IBInspectable and @IBDesignable to let you see it during Storyboard design:

import UIKit

@IBDesignable
class RoundedTextField: UIView {

@IBInspectable var placeholder: String = "" {
didSet {
textField.attributedPlaceholder = NSAttributedString(string: placeholder,
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white.withAlphaComponent(0.5)])
}
}

let textField: UITextField = {
let v = UITextField()
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()

override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}

override func prepareForInterfaceBuilder() {
commonInit()
}

func commonInit() -> Void {

layer.borderColor = UIColor.white.cgColor
layer.borderWidth = 2
layer.masksToBounds = true

addSubview(textField)

NSLayoutConstraint.activate([
textField.topAnchor.constraint(equalTo: topAnchor, constant: 12.0),
textField.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -12.0),
textField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20.0),
textField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20.0),
])

}

override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.size.height * 0.5
}

}

Result in Storyboard / Interface Builder:

Sample Image

Button border with corner radius in Swift UI

Instead of setting the cornerRadius to the Button use an overlay for the inside View:

Edit: If you have a background for the button you also need to apply the cornerRadius to the background.

    Button(action: {
print("sign up bin tapped")
}) {
Text("SIGN UP")
.frame(minWidth: 0, maxWidth: .infinity)
.font(.system(size: 18))
.padding()
.foregroundColor(.white)
.overlay(
RoundedRectangle(cornerRadius: 25)
.stroke(Color.white, lineWidth: 2)
)
}
.background(Color.yellow) // If you have this
.cornerRadius(25) // You also need the cornerRadius here

How to make TextEditor look like TextField - SwiftUI

This gets closer (no focus support, although that could be added):

TextEditor(text: $textBinding)
.padding(4)
.overlay(RoundedRectangle(cornerRadius: 8)
.stroke(Color.secondary).opacity(0.5))

I think the most important thing is making sure that the border is actually rounded -- right now, yours is getting cut off at the corners.



Related Topics



Leave a reply



Submit