Fix cursor-jump bug

This commit is contained in:
Angelo Stavrow 2020-11-10 15:08:55 -05:00
parent e5d844757a
commit 0b02903b26
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 3 additions and 22 deletions

View File

@ -5,7 +5,6 @@ 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,37 +15,22 @@ 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) {
DispatchQueue.main.async {
self.postBodyTextView.text = textView.text ?? ""
}
}
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.postBodyTextView.text = textView.text ?? ""
self.isFirstResponder = false
self.didBecomeFirstResponder = false
self.currentTextPosition = textView.selectedTextRange
}
func layoutManager(
@ -75,11 +59,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 +76,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 +87,6 @@ struct PostBodyTextView: UIViewRepresentable {
self,
text: $text,
isFirstResponder: $isFirstResponder,
currentTextPosition: $currentTextPosition,
lineSpacingMultiplier: lineSpacing
)
}

View File

@ -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