Skip to content

Instantly share code, notes, and snippets.

@Sangsom
Last active April 20, 2021 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sangsom/24c3dc68b371fe8475796735905777eb to your computer and use it in GitHub Desktop.
Save Sangsom/24c3dc68b371fe8475796735905777eb to your computer and use it in GitHub Desktop.
Sample code for UITextField representable blog post
struct ContentView: View {
var body: some View {
URLTextField()
.frame(height: 30)
.padding()
}
}
struct ContentView: View {
@State private var urlString = ""
var body: some View {
URLTextField(urlString: $urlString)
.frame(height: 30)
.padding()
}
}
import Foundation
import SwiftUI
struct URLTextField: UIViewRepresentable {
}
import Foundation
import SwiftUI
struct URLTextField: UIViewRepresentable {
func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.borderStyle = .roundedRect
textField.placeholder = "URL"
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.spellCheckingType = .no
textField.keyboardType = .URL
return textField
}
func updateUIView(_ view: UITextField, context: Context) {
}
}
struct URLTextField: UIViewRepresentable {
@Binding var urlString: String
func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.borderStyle = .roundedRect
textField.placeholder = "URL"
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.spellCheckingType = .no
textField.keyboardType = .URL
return textField
}
func updateUIView(_ view: UITextField, context: Context) {
view.text = urlString
}
}
struct URLTextField: UIViewRepresentable {
@Binding var urlString: String
func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.borderStyle = .roundedRect
textField.placeholder = "URL"
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.spellCheckingType = .no
textField.keyboardType = .URL
return textField
}
func updateUIView(_ view: UITextField, context: Context) {
view.text = urlString
}
}
struct URLTextField: UIViewRepresentable {
@Binding var urlString: String
func makeCoordinator() -> Coordinator {
Coordinator()
}
func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.borderStyle = .roundedRect
textField.placeholder = "URL"
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.spellCheckingType = .no
textField.keyboardType = .URL
textField.delegate = context.coordinator
return textField
}
func updateUIView(_ view: UITextField, context: Context) {
view.text = urlString
}
}
extension URLTextField {
class Coordinator: NSObject, UITextFieldDelegate {
}
}
import Foundation
import SwiftUI
struct URLTextField: UIViewRepresentable {
@Binding var urlString: String
func makeCoordinator() -> Coordinator {
Coordinator(text: $urlString)
}
func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.borderStyle = .roundedRect
textField.placeholder = "URL"
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.spellCheckingType = .no
textField.keyboardType = .URL
textField.delegate = context.coordinator
return textField
}
func updateUIView(_ view: UITextField, context: Context) {
view.text = urlString
}
}
extension URLTextField {
class Coordinator: NSObject, UITextFieldDelegate {
@Binding var text: String
init(text: Binding<String>) {
_text = text
}
func textFieldDidChangeSelection(_ textField: UITextField) {
DispatchQueue.main.async {
self.text = textField.text ?? ""
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment