-
-
Save Sangsom/24c3dc68b371fe8475796735905777eb to your computer and use it in GitHub Desktop.
Sample code for UITextField representable blog post
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 ContentView: View { | |
var body: some View { | |
URLTextField() | |
.frame(height: 30) | |
.padding() | |
} | |
} |
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 ContentView: View { | |
@State private var urlString = "" | |
var body: some View { | |
URLTextField(urlString: $urlString) | |
.frame(height: 30) | |
.padding() | |
} | |
} |
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
import Foundation | |
import SwiftUI | |
struct URLTextField: UIViewRepresentable { | |
} |
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
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) { | |
} | |
} |
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 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 | |
} | |
} |
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 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 | |
} | |
} |
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 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 { | |
} | |
} |
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
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