Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import SwiftUI
enum TextFieldTypes {
case defaultStyle
case customStyle
}
struct ContentView: View {
@State var myfield = ""
@ScaledMetric(relativeTo: .body) var imageSize = CGFloat(2)
@ScaledMetric(relativeTo: .body) var horizontalPadding: CGFloat = 4
@ScaledMetric(relativeTo: .body) var verticalPadding: CGFloat = 2
@ScaledMetric(relativeTo: .body) var cornerRadiusValue: CGFloat = 8
@State var textFieldTypes: TextFieldTypes = .defaultStyle
@State var text = ""
var body: some View {
VStack {
Picker("TextFieldStyle", selection: $textFieldTypes) {
Text("Original").tag(TextFieldTypes.defaultStyle)
Text("Custom").tag(TextFieldTypes.customStyle)
}
.pickerStyle(SegmentedPickerStyle())
.padding()
switch textFieldTypes {
case .defaultStyle:
HStack {
Text("DefaultTextField")
Spacer()
}.padding(.horizontal)
TextField("Label", text: $myfield)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding(.horizontal)
HStack {
Text("Plain")
Spacer()
}.padding(.horizontal)
TextField("Label", text: $myfield)
.textFieldStyle(DefaultTextFieldStyle())
.padding(.horizontal)
case .customStyle:
HStack {
Text("Rect")
Spacer()
}.padding(.horizontal)
TextField("Label", text: $myfield)
.padding(EdgeInsets(top: verticalPadding, leading: horizontalPadding, bottom: verticalPadding, trailing: horizontalPadding))
.background(
Rectangle()
.stroke(Color.gray, lineWidth: 0.333)
)
.padding(.horizontal)
HStack {
Text("RoundRect")
Spacer()
}.padding(.horizontal)
TextField("Label", text: $myfield)
.padding(EdgeInsets(top: verticalPadding,
leading: horizontalPadding + cornerRadiusValue * 0.5,
bottom: verticalPadding,
trailing: horizontalPadding + cornerRadiusValue * 0.5))
.background(
RoundedRectangle(cornerRadius: cornerRadiusValue)
.stroke(Color.gray, lineWidth: 0.333)
.foregroundColor(.clear)
)
.padding(.horizontal)
}
Spacer()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment