mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Merge pull request #125 from writeas/fix-cursor-jump-to-end-of-post-body
Fix cursor jump to end of post body
This commit is contained in:
commit
3fad7f38d2
@ -1,11 +1,10 @@
|
||||
// Based on https://stackoverflow.com/a/56508132/1234545 and https://stackoverflow.com/a/48360549/1234545
|
||||
// Based on https://stackoverflow.com/a/56508132 and https://stackoverflow.com/a/48360549
|
||||
|
||||
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
|
||||
@ -16,14 +15,12 @@ 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) {
|
||||
@ -33,20 +30,12 @@ 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(
|
||||
@ -75,11 +64,10 @@ 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 {
|
||||
let textView = UITextView(frame: .zero)
|
||||
let textView = UITextView()
|
||||
|
||||
textView.isEditable = true
|
||||
textView.isUserInteractionEnabled = true
|
||||
@ -93,6 +81,7 @@ struct PostBodyTextView: UIViewRepresentable {
|
||||
let font = textStyle
|
||||
let fontMetrics = UIFontMetrics(forTextStyle: .largeTitle)
|
||||
textView.font = fontMetrics.scaledFont(for: font)
|
||||
|
||||
textView.backgroundColor = UIColor.clear
|
||||
|
||||
return textView
|
||||
@ -103,7 +92,6 @@ struct PostBodyTextView: UIViewRepresentable {
|
||||
self,
|
||||
text: $text,
|
||||
isFirstResponder: $isFirstResponder,
|
||||
currentTextPosition: $currentTextPosition,
|
||||
lineSpacingMultiplier: lineSpacing
|
||||
)
|
||||
}
|
||||
|
@ -11,7 +11,6 @@ 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(
|
||||
@ -72,7 +71,6 @@ struct PostTextEditingView: View {
|
||||
text: $post.body,
|
||||
textStyle: $bodyTextStyle,
|
||||
isFirstResponder: $bodyIsFirstResponder,
|
||||
currentTextPosition: $bodyCursorPosition,
|
||||
lineSpacing: horizontalSizeClass == .compact ? lineSpacingMultiplier / 2 : lineSpacingMultiplier
|
||||
)
|
||||
.onChange(of: post.body) { _ in
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Based on https://lostmoa.com/blog/DynamicHeightForTextFieldInSwiftUI and https://stackoverflow.com/a/56508132/1234545
|
||||
// Based on https://lostmoa.com/blog/DynamicHeightForTextFieldInSwiftUI and https://stackoverflow.com/a/56508132
|
||||
|
||||
import SwiftUI
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user