Post

Replies

Boosts

Views

Activity

Text insertion in UIViewRepresentable-wrapped UITextField goes haywire after inserting emoji
Folks, I'm trying to understand what I'm missing here. In a SwiftUI project, I've wrapped a UITextView in UIViewRepresentable as a replacement for SwiftUI's TextEditor control, largely so that I can get it to become/resign first responder status. This minimally-viable example works fine, except for one bug that I can't seem to resolve: if I add an emoji anywhere in that text view, then text insertion goes haywire — wherever I have the cursor, the character typed will be inserted, but then the cursor immediately jumps to the end of the text in the field. import SwiftUI struct WrappedTextView: UIViewRepresentable { &#9;&#9;class Coordinator: NSObject, UITextViewDelegate { &#9;&#9;&#9;&#9;@Binding var text: String &#9;&#9;&#9;&#9;var didBecomeFirstResponder: Bool = false &#9;&#9;&#9;&#9;init(text: Binding<String>) { &#9;&#9;&#9;&#9;&#9;&#9;_text = text &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;func textViewDidChangeSelection(_ textView: UITextView) { &#9;&#9;&#9;&#9;&#9;&#9;DispatchQueue.main.async { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;self.text = textView.text ?? "" &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;@Binding var text: String &#9;&#9;var isFirstResponder: Bool = false &#9;&#9;func makeUIView(context: UIViewRepresentableContext<WrappedTextView>) -> UITextView { &#9;&#9;&#9;&#9;let textView = UITextView() &#9;&#9;&#9;&#9;textView.delegate = context.coordinator &#9;&#9;&#9;&#9;return textView &#9;&#9;} &#9;&#9;func makeCoordinator() -> WrappedTextView.Coordinator { &#9;&#9;&#9;&#9;return Coordinator(text: $text) &#9;&#9;} &#9;&#9;func updateUIView(_ uiView: UIViewType, context: UIViewRepresentableContext<WrappedTextView>) { &#9;&#9;&#9;&#9;uiView.text = text &#9;&#9;&#9;&#9;if isFirstResponder && !context.coordinator.didBecomeFirstResponder { &#9;&#9;&#9;&#9;&#9;&#9;uiView.becomeFirstResponder() &#9;&#9;&#9;&#9;&#9;&#9;context.coordinator.didBecomeFirstResponder = true &#9;&#9;&#9;&#9;} &#9;&#9;} } Credit to this Stack Overflow answer for the above code: https://stackoverflow.com/a/56508132 Can someone help me understand what I'm doing wrong? There's an example project available here, wrapping both UITextField and UITextView in UIViewRepresentable: https://github.com/AngeloStavrow/WrappedTextInputExample
1
0
2.3k
Nov ’20
How to listen for didFinishLaunchingNotification in SwiftUI?
Hi all, I'm trying to detect an app launch in a multiplatform SwiftUI app (iOS 14/macOS 11) and having a heck of a time getting onReceive() handlers to catch didFinishLaunchingNotification, despite other notifications are being received as expected. Here's a simplified version of what I'm trying: import SwiftUI struct ContentView: View { &#9;&#9;#if os(iOS) &#9;&#9;let didFinishLaunchingNotification = UIApplication.didFinishLaunchingNotification &#9;&#9;let didBecomeActiveNotification = UIApplication.didBecomeActiveNotification &#9;&#9;#else &#9;&#9;let didFinishLaunchingNotification = NSApplication.didFinishLaunchingNotification &#9;&#9;let didBecomeActiveNotification = NSApplication.didBecomeActiveNotification &#9;&#9;#endif &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;NavigationView { &#9;&#9;&#9;&#9;&#9;&#9;SidebarView() &#9;&#9;&#9;&#9;&#9;&#9;ListView() &#9;&#9;&#9;&#9;&#9;&#9;Text("Content!") &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.onReceive(NotificationCenter.default.publisher(for: didFinishLaunchingNotification)) { _ in &#9;&#9;&#9;&#9;&#9;&#9;print("didFinishLaunchingNotification fired") &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;.onReceive(NotificationCenter.default.publisher(for: didBecomeActiveNotification)) { _ in &#9;&#9;&#9;&#9;&#9;&#9;print("didBecomeActiveNotification fired") &#9;&#9;&#9;&#9;} &#9;&#9;} } Do you all see anything wrong with this approach? Is this a bug in SwiftUI? Thanks in advance!
1
0
1.3k
Sep ’20