Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for disabling scrolling on textView in RichTextViewController #32

Open
charlemagneVI opened this issue Apr 8, 2022 · 1 comment

Comments

@charlemagneVI
Copy link

When using RichTextViewController with a UIViewRepresentable in SwiftUI, it would be helpful to be able to control whether the textView content is scrollable in the view controller.

The issue that occurs is that if you ever want to include the RichTextViewController amongst other SwiftUI content, the scroll behavior is always determined by the hardcoded value on textView.

If you have a scenario where your SwiftUI view looks like this:

struct ViewWithScroll: View {
    var body: some View {
        ScrollView {
            Text("Some Text")

            RichTextViewRepresentable()

            Button(action: {}) {
                Text("Some Other Content")
            }
        }
    }
}

The text content contained in RichTextViewRepresentable() has separate scroll behavior from the containing ScrollView

You can find my quick implementation in my fork here:
https://github.com/CharlemagneVI/rich-text-renderer.swift/commit/b7352ce83c456c110435044ee490690d3670e2fe

@startover205
Copy link

startover205 commented May 20, 2022

Good solution! But some of my content got clipped if I disable scrolling.

Scrolling enabled Scrolling disabled

My workaround is to move the change to the implementation of renderDocumentIfNeeded of RichTextViewController.
Make the text view scrollable before rendering and disable the scrolling after rendering is done.

private func renderDocumentIfNeeded() {
    guard let document = richTextDocument else { return }
    
    textView.isScrollEnabled = true

    DispatchQueue.main.async {
        let output = self.renderer.render(document: document)
        self.textStorage.beginEditing()
        self.textStorage.setAttributedString(output)
        self.textStorage.endEditing()
        self.calculateAndSetPreferredContentSize()
        
        self.textView.isScrollEnabled = self.renderer.configuration.isScrollable
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants