Skip to content

Commit 51f8495

Browse files
committed
Make Return key navigate from title field to last body field position
1 parent c44d48e commit 51f8495

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

iOS/PostEditor/PostBodyTextView.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import SwiftUI
55
class PostBodyCoordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegate {
66
@Binding var text: String
77
@Binding var isFirstResponder: Bool
8+
@Binding var currentTextPosition: UITextRange?
89
var lineSpacingMultiplier: CGFloat
910
var didBecomeFirstResponder: Bool = false
1011
var postBodyTextView: PostBodyTextView
@@ -15,12 +16,14 @@ class PostBodyCoordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegate
1516
_ textView: PostBodyTextView,
1617
text: Binding<String>,
1718
isFirstResponder: Binding<Bool>,
19+
currentTextPosition: Binding<UITextRange?>,
1820
lineSpacingMultiplier: CGFloat
1921
) {
2022
self.postBodyTextView = textView
2123
_text = text
2224
_isFirstResponder = isFirstResponder
2325
self.lineSpacingMultiplier = lineSpacingMultiplier
26+
_currentTextPosition = currentTextPosition
2427
}
2528

2629
func textViewDidChange(_ textView: UITextView) {
@@ -29,6 +32,23 @@ class PostBodyCoordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegate
2932
}
3033
}
3134

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+
3252
func layoutManager(
3353
_ layoutManager: NSLayoutManager,
3454
lineSpacingAfterGlyphAt glyphIndex: Int,
@@ -55,6 +75,7 @@ struct PostBodyTextView: UIViewRepresentable {
5575
@Binding var text: String
5676
@Binding var textStyle: UIFont
5777
@Binding var isFirstResponder: Bool
78+
@Binding var currentTextPosition: UITextRange?
5879
@State var lineSpacing: CGFloat
5980

6081
func makeUIView(context: UIViewRepresentableContext<PostBodyTextView>) -> UITextView {
@@ -82,6 +103,7 @@ struct PostBodyTextView: UIViewRepresentable {
82103
self,
83104
text: $text,
84105
isFirstResponder: $isFirstResponder,
106+
currentTextPosition: $currentTextPosition,
85107
lineSpacingMultiplier: lineSpacing
86108
)
87109
}

iOS/PostEditor/PostTextEditingView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct PostTextEditingView: View {
1111
@State private var titleIsFirstResponder: Bool = true
1212
@State private var bodyTextStyle: UIFont = UIFont(name: "Lora-Regular", size: 17)!
1313
@State private var bodyIsFirstResponder: Bool = false
14+
@State private var bodyCursorPosition: UITextRange?
1415
private let lineSpacingMultiplier: CGFloat = 0.5
1516

1617
init(
@@ -71,6 +72,7 @@ struct PostTextEditingView: View {
7172
text: $post.body,
7273
textStyle: $bodyTextStyle,
7374
isFirstResponder: $bodyIsFirstResponder,
75+
currentTextPosition: $bodyCursorPosition,
7476
lineSpacing: horizontalSizeClass == .compact ? lineSpacingMultiplier / 2 : lineSpacingMultiplier
7577
)
7678
.onChange(of: post.body) { _ in

iOS/PostEditor/PostTitleTextView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class PostTitleCoordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegat
3232
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
3333
if text == "\n" {
3434
self.isFirstResponder.toggle()
35+
self.didBecomeFirstResponder = false
3536
return false
3637
}
3738
return true

0 commit comments

Comments
 (0)