Created
November 30, 2019 21:15
-
-
Save plu/657d18db8e4f60d22c3d030ca84a6d35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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