Skip to content

Instantly share code, notes, and snippets.

@plu
Created November 30, 2019 21:15
Show Gist options
  • Save plu/657d18db8e4f60d22c3d030ca84a6d35 to your computer and use it in GitHub Desktop.
Save plu/657d18db8e4f60d22c3d030ca84a6d35 to your computer and use it in GitHub Desktop.
public struct UIKitConfiguration<T: UIView> {
public let apply: (T) -> Void
public init(_ apply: @escaping (T) -> Void) {
self.apply = apply
}
}
public extension UIKitConfiguration where T: UIKitTextField.View {
static var zipCode: UIKitConfiguration {
return .init { textField in
textField.autocorrectionType = .no
textField.clearButtonMode = .whileEditing
textField.font = UIFont.systemFont(ofSize: UIFont.labelFontSize, weight: .semibold)
textField.keyboardType = .numberPad
textField.placeholder = "Enter something..."
textField.textColor = .red
}
}
}
public struct UIKitTextField: UIViewRepresentable {
public init(
configuration: UIKitConfiguration<UIKitTextField.View>,
) {
self.configuration = configuration
}
public func makeUIView(context: UIViewRepresentableContext<UIKitTextField>) -> View {
let textField = View()
textField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
configuration.apply(textField)
return textField
}
private let configuration: UIKitConfiguration<View>
}
public extension UIKitTextField {
final class View: UITextField {
}
}
struct FormTextField: View {
var body: some View {
UIKitTextField(configuration: .zipCode)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment