prndcompany/swiftuihtml
**SwiftUIHTML** is a powerful and customizable library for rendering HTML content as native views in SwiftUI.
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
π¦ 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:
- π Basic Usage - HTML rendering basics
- π¨ Styling Guide - CSS styles and configuration
- π§ Custom Tags - Creating custom tags
- π Parser Integration - Fuzi, SwiftSoup integration
- π Advanced Features - Environment values and advanced customization
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.
Package Metadata
Repository: prndcompany/swiftuihtml
Default branch: main
README: README.md