Skip to content

PRNDcompany/SwiftUIHTML

Repository files navigation

SwiftUIHTML

Swift SPM License

SwiftUIHTML is a powerful and customizable library for rendering HTML content as native views in SwiftUI.

ν•œκΈ€ λ¬Έμ„œ 보기 (Korean Documentation)

Demo

SwiftUIHTML Demo


πŸš€ Key Features

  • HTML Rendering: Convert HTML to native SwiftUI views
  • Custom Tag System: Extensible through BlockTag, InlineTag, and InlineAttachmentTag protocols
  • CSS Style Support: Full inline style support (padding, margin, background, border, etc.)
  • Flexible Parser Integration: Works with external parsers like Fuzi and SwiftSoup
  • Environment Value System: Global configuration and style customization

πŸ“‹ Supported Tags

Built-in Tags

Category Tags
Block div, body, p, header, main, section, footer, h1, h2
Inline span, a, b, strong, i, em, u
Attachment img

Note: Tags like h3, ul, video can be registered as custom tags.

CSS Style Properties

  • Text Styles: color, background-color, font-family, font-size, line-height, word-break
  • Block Layout: padding, margin, border, border-radius (block elements only: div, p, section, etc.)
  • Inline Styles: color, background-color, border-radius (inline elements: strong, em, span, etc.)

Note: padding and margin are not supported for inline elements (span, strong, em, etc.).


πŸ“¦ Installation

Swift Package Manager (SPM)

dependencies: [
    .package(url: "https://github.com/PRNDcompany/SwiftUIHTML.git", from: "1.0.0"),
],
targets: [
    .target(name: "YourTarget", dependencies: ["SwiftUIHTML"]),
]

πŸ› οΈ Quick Start

Basic Usage

import SwiftUI
import SwiftUIHTML

struct ContentView: View {
    let html = """
        <h1>Hello, SwiftUIHTML!</h1>
        <p>This is a <strong>paragraph</strong> with <em>styled</em> text.</p>
        <img src="https://example.com/image.jpg" width="100" height="100" />
        """
    
    var body: some View {
        HTMLView(html: html, parser: HTMLFuziParser())
            .htmlEnvironment(\.configuration, .default)
            .htmlEnvironment(\.styleContainer, createStyleContainer())
    }
    
    func createStyleContainer() -> HTMLStyleContainer {
        var container = HTMLStyleContainer()
        container.uiFont = .systemFont(ofSize: 16)
        container.lineBreakMode = .byWordWrapping
        return container
    }
}

Parser Implementation

You can use any HTML parser by implementing the HTMLParserable protocol:

struct MyHTMLParser: HTMLParserable {
    func parse(html: String) -> HTMLNode {
        // Parser implementation
    }
}

πŸ“š Detailed parser implementation examples: Documentation/ParserIntegration.md


πŸ“š Documentation

For detailed usage and examples, please refer to the Documentation folder:

Quick Examples

Register Custom Tag

// Simple custom tag registration
let configuration = HTMLConfiguration.default
    .register(tag: "video", renderer: VideoTag.self)
    .register(tag: "h3", renderer: HeadingLevel3.self)

Apply CSS Styles

let html = """
    <div style="padding: 20px; background-color: #f0f0f0; border-radius: 8px;">
        <h2 style="color: #333;">Style Example</h2>
    </div>
    """

Line Break Mode

var container = HTMLStyleContainer()
container.lineBreakMode = .byWordWrapping  // or .byCharWrapping

πŸ” Key Components

HTMLView

Main view for rendering HTML content

HTMLConfiguration

Register and manage tag renderers

HTMLStyleContainer

Global text style configuration

HTMLParserable

Protocol for external HTML parser integration


πŸ“± Example Project

For more examples, please refer to the project in the Example folder.


🀝 Contributing

Contributions are welcome! Feel free to submit issues and pull requests.


πŸ“„ License

SwiftUIHTML is released under the MIT License. See LICENSE for details.

About

SwiftUIHTML

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages