/WPArticleView

SwiftUI View for Wordpress JSON API

Primary LanguageSwift

WPArticleView

Installation

...
dependencies: [
    .package(url: "https://github.com/nightwill/WPArticleView.git", from: "0.1.0"),
],
targets: [
    .target( name: "YourTarget", dependencies: ["WPArticleView"]),
]
...

Usage:

import SwiftUI
import WPArticleView

struct ContentView: View {

    let htmlText: String
        
    var body: some View {
        WPArticleView(htmlBody: htmlText) { blocks in
            
            List(blocks.indices, id: \.self) { id in
                blocks[id]
            }
            
        } attributedText: { attributedText in
            
            Text(attributedText)
            
        } image: { imageURL in
            
            AsyncImage(url: imageURL) { image in
                image.resizable()
                    .aspectRatio(contentMode: .fill)
            } placeholder: {
                ProgressView()
            }
            
        } video: { videoURL in
                    
            VideoPlayer(player: AVPlayer(url: videoURL))
                    .frame(minHeight: 400)
                    
        }
    }
    
}

Or using ScrollView:

import SwiftUI
import WPArticleView

struct ContentView: View {
        
    var body: some View {
        ScrollView {
            LazyVStack(alignment: .leading, spacing: 16) {
                WPArticleView(htmlBody: htmlText) { blocks in
                    
                    ForEach(blocks.indices, id: \.self) { id in
                        blocks[id]
                    }
                    
                    
                } attributedText: { attributedText in
                    
                    Text(attributedText)
                    
                } image: { imageURL in
                    
                    AsyncImage(url: imageURL) { image in
                        image.resizable()
                            .aspectRatio(contentMode: .fill)
                    } placeholder: {
                        ProgressView()
                            .frame(maxWidth: .infinity)
                    }
                    
                } video: { videoURL in
                    
                    VideoPlayer(player: AVPlayer(url: videoURL))
                        .frame(minHeight: 400)
                    
                }
            }
            .padding(24)
        }
        
    }
    
}