|
| 1 | +// Based on https://stackoverflow.com/a/56508132/1234545 and https://stackoverflow.com/a/48360549/1234545 |
| 2 | + |
| 3 | +import SwiftUI |
| 4 | + |
| 5 | +class PostBodyCoordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegate { |
| 6 | + @Binding var text: String |
| 7 | + @Binding var isFirstResponder: Bool |
| 8 | + @Binding var currentTextPosition: UITextRange? |
| 9 | + var lineSpacingMultiplier: CGFloat |
| 10 | + var didBecomeFirstResponder: Bool = false |
| 11 | + var postBodyTextView: PostBodyTextView |
| 12 | + |
| 13 | + weak var textView: UITextView? |
| 14 | + |
| 15 | + init( |
| 16 | + _ textView: PostBodyTextView, |
| 17 | + text: Binding<String>, |
| 18 | + isFirstResponder: Binding<Bool>, |
| 19 | + currentTextPosition: Binding<UITextRange?>, |
| 20 | + lineSpacingMultiplier: CGFloat |
| 21 | + ) { |
| 22 | + self.postBodyTextView = textView |
| 23 | + _text = text |
| 24 | + _isFirstResponder = isFirstResponder |
| 25 | + self.lineSpacingMultiplier = lineSpacingMultiplier |
| 26 | + _currentTextPosition = currentTextPosition |
| 27 | + } |
| 28 | + |
| 29 | + func textViewDidChange(_ textView: UITextView) { |
| 30 | + DispatchQueue.main.async { |
| 31 | + self.postBodyTextView.text = textView.text ?? "" |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { |
| 36 | + self.currentTextPosition = textView.selectedTextRange |
| 37 | + return true |
| 38 | + } |
| 39 | + |
| 40 | + func textViewDidBeginEditing(_ textView: UITextView) { |
| 41 | + if let textPosition = currentTextPosition { |
| 42 | + textView.selectedTextRange = textPosition |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + func textViewDidEndEditing(_ textView: UITextView) { |
| 47 | + self.isFirstResponder = false |
| 48 | + self.didBecomeFirstResponder = false |
| 49 | + self.currentTextPosition = textView.selectedTextRange |
| 50 | + } |
| 51 | + |
| 52 | + func layoutManager( |
| 53 | + _ layoutManager: NSLayoutManager, |
| 54 | + lineSpacingAfterGlyphAt glyphIndex: Int, |
| 55 | + withProposedLineFragmentRect rect: CGRect |
| 56 | + ) -> CGFloat { |
| 57 | + // HACK: - This seems to be the only way to get line spacing to update dynamically on iPad |
| 58 | + // when switching between full-screen, split-screen, and slide-over views. |
| 59 | + if let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first { |
| 60 | + // Get the width of the window to determine the size class |
| 61 | + if window.frame.width < 600 { |
| 62 | + // Use 0.25 multiplier for compact size class |
| 63 | + return 17 * 0.25 |
| 64 | + } else { |
| 65 | + // Use 0.5 multiplier otherwise |
| 66 | + return 17 * 0.5 |
| 67 | + } |
| 68 | + } else { |
| 69 | + return 17 * lineSpacingMultiplier |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +struct PostBodyTextView: UIViewRepresentable { |
| 75 | + @Binding var text: String |
| 76 | + @Binding var textStyle: UIFont |
| 77 | + @Binding var isFirstResponder: Bool |
| 78 | + @Binding var currentTextPosition: UITextRange? |
| 79 | + @State var lineSpacing: CGFloat |
| 80 | + |
| 81 | + func makeUIView(context: UIViewRepresentableContext<PostBodyTextView>) -> UITextView { |
| 82 | + let textView = UITextView(frame: .zero) |
| 83 | + |
| 84 | + textView.isEditable = true |
| 85 | + textView.isUserInteractionEnabled = true |
| 86 | + textView.isScrollEnabled = true |
| 87 | + textView.alwaysBounceVertical = false |
| 88 | + |
| 89 | + context.coordinator.textView = textView |
| 90 | + textView.delegate = context.coordinator |
| 91 | + textView.layoutManager.delegate = context.coordinator |
| 92 | + |
| 93 | + let font = textStyle |
| 94 | + let fontMetrics = UIFontMetrics(forTextStyle: .largeTitle) |
| 95 | + textView.font = fontMetrics.scaledFont(for: font) |
| 96 | + textView.backgroundColor = UIColor.clear |
| 97 | + |
| 98 | + return textView |
| 99 | + } |
| 100 | + |
| 101 | + func makeCoordinator() -> PostBodyCoordinator { |
| 102 | + return Coordinator( |
| 103 | + self, |
| 104 | + text: $text, |
| 105 | + isFirstResponder: $isFirstResponder, |
| 106 | + currentTextPosition: $currentTextPosition, |
| 107 | + lineSpacingMultiplier: lineSpacing |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<PostBodyTextView>) { |
| 112 | + uiView.text = text |
| 113 | + |
| 114 | + let font = textStyle |
| 115 | + let fontMetrics = UIFontMetrics(forTextStyle: .largeTitle) |
| 116 | + uiView.font = fontMetrics.scaledFont(for: font) |
| 117 | + |
| 118 | + // We don't want the text field to become first responder every time SwiftUI refreshes the view. |
| 119 | + if isFirstResponder && !context.coordinator.didBecomeFirstResponder { |
| 120 | + uiView.becomeFirstResponder() |
| 121 | + context.coordinator.didBecomeFirstResponder = true |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments