From 52618095f2c1d19e2e79141979504bf0aa3d1d83 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Thu, 29 Dec 2022 11:44:19 -0500 Subject: [PATCH] Set the font in the updateUIView method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also, wrap becomeFirstResponder in async dispatch This fixes issues when we’re updating the responder chain every time SwiftUI refreshes the view, which can have unpredictable side effects. --- iOS/PostEditor/MultilineTextView.swift | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/iOS/PostEditor/MultilineTextView.swift b/iOS/PostEditor/MultilineTextView.swift index b3afd97..ffffa18 100644 --- a/iOS/PostEditor/MultilineTextView.swift +++ b/iOS/PostEditor/MultilineTextView.swift @@ -17,17 +17,12 @@ private struct UITextViewWrapper: UIViewRepresentable { textField.delegate = context.coordinator textField.isEditable = true - textField.font = UIFont.preferredFont(forTextStyle: .body) textField.isSelectable = true textField.isUserInteractionEnabled = true textField.isScrollEnabled = false textField.backgroundColor = UIColor.clear textField.smartDashesType = .no - let font = textStyle - let fontMetrics = UIFontMetrics(forTextStyle: .largeTitle) - textField.font = fontMetrics.scaledFont(for: font) - if nil != onDone { textField.returnKeyType = .next } @@ -41,8 +36,14 @@ private struct UITextViewWrapper: UIViewRepresentable { uiView.text = self.text } - if uiView.window != nil, isEditing { - uiView.becomeFirstResponder() + let font = textStyle + let fontMetrics = UIFontMetrics(forTextStyle: .largeTitle) + uiView.font = fontMetrics.scaledFont(for: font) + + if uiView.window != nil && isEditing { + DispatchQueue.main.async { + uiView.becomeFirstResponder() + } } UITextViewWrapper.recalculateHeight(view: uiView, result: $calculatedHeight)