mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Make Return key navigate from title field to last body field position
This commit is contained in:
parent
c44d48e526
commit
51f8495a06
@ -5,6 +5,7 @@ import SwiftUI
|
||||
class PostBodyCoordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegate {
|
||||
@Binding var text: String
|
||||
@Binding var isFirstResponder: Bool
|
||||
@Binding var currentTextPosition: UITextRange?
|
||||
var lineSpacingMultiplier: CGFloat
|
||||
var didBecomeFirstResponder: Bool = false
|
||||
var postBodyTextView: PostBodyTextView
|
||||
@ -15,12 +16,14 @@ class PostBodyCoordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegate
|
||||
_ textView: PostBodyTextView,
|
||||
text: Binding<String>,
|
||||
isFirstResponder: Binding<Bool>,
|
||||
currentTextPosition: Binding<UITextRange?>,
|
||||
lineSpacingMultiplier: CGFloat
|
||||
) {
|
||||
self.postBodyTextView = textView
|
||||
_text = text
|
||||
_isFirstResponder = isFirstResponder
|
||||
self.lineSpacingMultiplier = lineSpacingMultiplier
|
||||
_currentTextPosition = currentTextPosition
|
||||
}
|
||||
|
||||
func textViewDidChange(_ textView: UITextView) {
|
||||
@ -29,6 +32,23 @@ class PostBodyCoordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegate
|
||||
}
|
||||
}
|
||||
|
||||
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
||||
self.currentTextPosition = textView.selectedTextRange
|
||||
return true
|
||||
}
|
||||
|
||||
func textViewDidBeginEditing(_ textView: UITextView) {
|
||||
if let textPosition = currentTextPosition {
|
||||
textView.selectedTextRange = textPosition
|
||||
}
|
||||
}
|
||||
|
||||
func textViewDidEndEditing(_ textView: UITextView) {
|
||||
self.isFirstResponder = false
|
||||
self.didBecomeFirstResponder = false
|
||||
self.currentTextPosition = textView.selectedTextRange
|
||||
}
|
||||
|
||||
func layoutManager(
|
||||
_ layoutManager: NSLayoutManager,
|
||||
lineSpacingAfterGlyphAt glyphIndex: Int,
|
||||
@ -55,6 +75,7 @@ struct PostBodyTextView: UIViewRepresentable {
|
||||
@Binding var text: String
|
||||
@Binding var textStyle: UIFont
|
||||
@Binding var isFirstResponder: Bool
|
||||
@Binding var currentTextPosition: UITextRange?
|
||||
@State var lineSpacing: CGFloat
|
||||
|
||||
func makeUIView(context: UIViewRepresentableContext<PostBodyTextView>) -> UITextView {
|
||||
@ -82,6 +103,7 @@ struct PostBodyTextView: UIViewRepresentable {
|
||||
self,
|
||||
text: $text,
|
||||
isFirstResponder: $isFirstResponder,
|
||||
currentTextPosition: $currentTextPosition,
|
||||
lineSpacingMultiplier: lineSpacing
|
||||
)
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ struct PostTextEditingView: View {
|
||||
@State private var titleIsFirstResponder: Bool = true
|
||||
@State private var bodyTextStyle: UIFont = UIFont(name: "Lora-Regular", size: 17)!
|
||||
@State private var bodyIsFirstResponder: Bool = false
|
||||
@State private var bodyCursorPosition: UITextRange?
|
||||
private let lineSpacingMultiplier: CGFloat = 0.5
|
||||
|
||||
init(
|
||||
@ -71,6 +72,7 @@ struct PostTextEditingView: View {
|
||||
text: $post.body,
|
||||
textStyle: $bodyTextStyle,
|
||||
isFirstResponder: $bodyIsFirstResponder,
|
||||
currentTextPosition: $bodyCursorPosition,
|
||||
lineSpacing: horizontalSizeClass == .compact ? lineSpacingMultiplier / 2 : lineSpacingMultiplier
|
||||
)
|
||||
.onChange(of: post.body) { _ in
|
||||
|
@ -32,6 +32,7 @@ class PostTitleCoordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegat
|
||||
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
|
||||
if text == "\n" {
|
||||
self.isFirstResponder.toggle()
|
||||
self.didBecomeFirstResponder = false
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
Loading…
Reference in New Issue
Block a user