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

Support for SwiftUI #37

Open
kamila-and opened this issue Jul 14, 2022 · 5 comments
Open

Support for SwiftUI #37

kamila-and opened this issue Jul 14, 2022 · 5 comments

Comments

@kamila-and
Copy link

Hi, will this library or another library ever support SwiftUI? Or is there anyone who successfully managed to integrate this library in their SwiftUI project that could help me out? Thanks

@PiotrBogus
Copy link

PiotrBogus commented Aug 20, 2023

import Foundation
import SwiftUI
import Contentful

@available(iOS 13.0, *)
public struct SwiftUIRichTextView: UIViewControllerRepresentable {
    private let richTextViewController: RichTextViewController
    @Binding private var richTextDocument: RichTextDocument?

    public init(
        richTextViewController: RichTextViewController,
        richTextDocument: Binding<RichTextDocument?> = .constant(nil)
    ) {
        self.richTextViewController = richTextViewController
        _richTextDocument = richTextDocument
    }

    public func makeUIViewController(context _: Context) -> RichTextViewController {
        return richTextViewController
    }

    public func updateUIViewController(_ viewController: RichTextViewController, context _: Context) {
        viewController.richTextDocument = richTextDocument
    }
}

@PiotrBogus
Copy link

Here is example how to use it:

@available(iOS 13.0, *)
struct SwiftUIMainView: View {
    private var richTextViewController: RichTextViewController {
        var configuration = DefaultRendererConfiguration()
        configuration.resourceLinkBlockViewProvider = ExampleBlockViewProvider()
        configuration.resourceLinkInlineStringProvider = ExampleInlineStringProvider()

        let renderersProvider = DefaultRenderersProvider()

        let renderer = RichTextDocumentRenderer(
            configuration: configuration,
            nodeRenderers: renderersProvider
        )

        return RichTextViewController(renderer: renderer)
    }
    
    private let client = ContentfulService()
    @State var richTextDocument: RichTextDocument?
    
    var body: some View {
        VStack {
            SwiftUIRichTextView(
                richTextViewController: richTextViewController,
                richTextDocument: $richTextDocument
            )
        }.onAppear {
            fetchContent()
        }
    }
    
    private func fetchContent() {
        client.fetchArticle { result in
            switch result {
            case .success(let article):
                print(article)
                richTextDocument = article.content

            case .failure(let error):
                print(error)
            }
        }
    }
}

@TizianoCoroneo
Copy link

@PiotrBogus in this example, ExampleBlockViewProvider() and ExampleInlineStringProvider() are required to provide UIView objects to render embedded entries inside rich text.
Is there any way to provide SwiftUI views? If the library changed these "provider" objects to provide UIViewControllers instead it would be trivial to return UIHostingController instead, solving the issue.

@TizianoCoroneo
Copy link

CC @mariuskatcontentful

@alizeec
Copy link

alizeec commented Sep 25, 2024

Hi, I have the same issue.
If I follow your code @PiotrBogus , I just have white rectangles

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

4 participants