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()
Corner Radius of both HStack and TextField not updating
The problem is here:
.padding() /// 1.
.background(Color(.systemGray6)) /// 2.
.padding() /// 3.
.cornerRadius(12) /// 4.
- Your text field has a
padding
background
gets applied, after thepadding
- You add another
padding
, after thebackground
- You apply another
cornerRadius
on top ofpadding
As a result, it's the padding
that gets rounded, not the background
.
Instead, you want to apply the cornerRadius
immediately after the background
.
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:
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)
}
}
}
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:
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
Swift: How to Create a Predicate with an Int Value
Recursion Over a Swift Sliceable
Operation Not Permitted When Executing 'Killall' with Swift
How to Pass Binding to Child View in the New Navigationstack.Navigationdestination
Play Segment of Avaudiopcmbuffer
Remove \\U{E2} Characters from String
Given a Function Parameter of Type [Int]; Can It Be Constrained to Not Be Empty
Why Is Casting a Struct to Anyobject Not a Compile Error in Swift
Swift Seems to Be Slower as Objective-C in Loops
Setting Nsunderlinestyle Causes Unrecogognized Selector Exception
Change Width of a Uibarbuttonitem in a Uinavigationbar in Swift
Handling an Attribute of an Xml Element in Swift
Why Does My @Lazy Property Crash, But If I Make It Non Lazy It Works
How to Use Tabs to Evenly Space Out Description Strings in Swift
How to Re-Order the Realm Table Using Tableview in Swift
Text from [String] to a Label.Text Isn't Working the First Time