SwiftUIHTML is a powerful and customizable library for rendering HTML content as native views in SwiftUI.
한글 문서 보기 (Korean Documentation)
- 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
| 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.
- 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:
paddingandmarginare not supported for inline elements (span, strong, em, etc.).
dependencies: [
.package(url: "https://github.com/PRNDcompany/SwiftUIHTML.git", from: "1.0.0"),
],
targets: [
.target(name: "YourTarget", dependencies: ["SwiftUIHTML"]),
]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
}
}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
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
// Simple custom tag registration
let configuration = HTMLConfiguration.default
.register(tag: "video", renderer: VideoTag.self)
.register(tag: "h3", renderer: HeadingLevel3.self)let html = """
<div style="padding: 20px; background-color: #f0f0f0; border-radius: 8px;">
<h2 style="color: #333;">Style Example</h2>
</div>
"""var container = HTMLStyleContainer()
container.lineBreakMode = .byWordWrapping // or .byCharWrappingMain view for rendering HTML content
Register and manage tag renderers
Global text style configuration
Protocol for external HTML parser integration
For more examples, please refer to the project in the Example folder.
Contributions are welcome! Feel free to submit issues and pull requests.
SwiftUIHTML is released under the MIT License. See LICENSE for details.