Skip to content

Instantly share code, notes, and snippets.

@Sangsom

Sangsom/ContentView_1.swift Secret

Last active Apr 20, 2021
Embed
What would you like to do?
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