SwiftUIHTML์ ๊ธฐ๋ณธ์ ์ธ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์๊ฐํฉ๋๋ค. This guide introduces the basic usage of SwiftUIHTML.
๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ HTML ๋ ๋๋ง ์์ ์ ๋๋ค. HTMLView๋ฅผ ์์ฑํ๊ณ ํ์๋ฅผ ์ง์ ํ๋ฉด ๋ฉ๋๋ค.
The most basic HTML rendering example. Simply create an HTMLView and specify a parser.
import SwiftUI
import SwiftUIHTML
struct SimpleHTMLView: View {
let html = """
<h1>์๋
ํ์ธ์!</h1>
<p>์ด๊ฒ์ <strong>SwiftUIHTML</strong> ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.</p>
"""
var body: some View {
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
}
}๋ค์ํ ํ ์คํธ ์คํ์ผ์ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋๋ค. ๋ณผ๋, ์ดํค๋ฆญ, ์ธ๋๋ผ์ธ ๋ฑ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
How to apply various text styles. You can use bold, italic, underline, and more.
struct TextStyleExample: View {
let html = """
<p>
์ผ๋ฐ ํ
์คํธ์ <b>๋ณผ๋ ํ
์คํธ</b>,
<i>์ดํค๋ฆญ ํ
์คํธ</i>,
<u>๋ฐ์ค ํ
์คํธ</u>๋ฅผ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค.
</p>
<p>
<strong>strong ํ๊ทธ</strong>์ <em>em ํ๊ทธ</em>๋ ์ง์ํฉ๋๋ค.
</p>
"""
var body: some View {
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.htmlEnvironment(\.styleContainer, {
var container = HTMLStyleContainer()
container.uiFont = .systemFont(ofSize: 16)
return container
}())
}
}HTML ๋ด์ ์ด๋ฏธ์ง๋ฅผ ์ฝ์ ํ๋ ๋ฐฉ๋ฒ์ ๋๋ค. ์น URL์ด๋ ๋ก์ปฌ ์ด๋ฏธ์ง๋ฅผ ๋ชจ๋ ์ง์ํฉ๋๋ค.
How to embed images in HTML. Both web URLs and local images are supported.
struct ImageExample: View {
let html = """
<h2>์ด๋ฏธ์ง ์์ </h2>
<p>ํ
์คํธ์ ํจ๊ป ์ด๋ฏธ์ง๋ฅผ ํ์ํฉ๋๋ค:</p>
<img src="https://picsum.photos/200/150" width="200" height="150" />
<p>์ด๋ฏธ์ง ์๋ ํ
์คํธ์
๋๋ค.</p>
<p>์ธ๋ผ์ธ ์ด๋ฏธ์ง๋ ๊ฐ๋ฅํฉ๋๋ค:
<img src="https://picsum.photos/20/20" width="20" height="20" />
ํ
์คํธ ์ค๊ฐ์ ์ฝ์
๋ฉ๋๋ค.</p>
"""
var body: some View {
ScrollView {
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.padding()
}
}
}ํด๋ฆญ ๊ฐ๋ฅํ ๋งํฌ๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋๋ค. ๋งํฌ ํ๊ทธ๋ ์๋์ผ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค.
How to create clickable links. Link tags are automatically handled.
struct LinkExample: View {
let html = """
<h2>๋งํฌ ์์ </h2>
<p>
์น์ฌ์ดํธ ๋งํฌ: <a href="https://www.apple.com">Apple ํํ์ด์ง</a>
</p>
<p>
์ด๋ฉ์ผ ๋งํฌ: <a href="mailto:example@email.com">์ด๋ฉ์ผ ๋ณด๋ด๊ธฐ</a>
</p>
"""
var body: some View {
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
}
}๋ค์ํ ๋ธ๋ก ์์๋ค์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋๋ค. div, p, section ๋ฑ์ ๊ธฐ๋ณธ ๋ธ๋ก ํ๊ทธ๋ฅผ ํ์ฉํฉ๋๋ค.
How to use various block elements. Utilize basic block tags like div, p, section, etc.
struct BlockElementExample: View {
let html = """
<div>
<h1>๋ฉ์ธ ์ ๋ชฉ</h1>
<p>์ฒซ ๋ฒ์งธ ๋จ๋ฝ์
๋๋ค.</p>
<p>๋ ๋ฒ์งธ ๋จ๋ฝ์
๋๋ค.</p>
</div>
<section>
<h2>์น์
์ ๋ชฉ</h2>
<div style="background-color: #f0f0f0; padding: 10px;">
<p>๋ฐฐ๊ฒฝ์์ด ์๋ div ์์ ๋จ๋ฝ์
๋๋ค.</p>
<p>์ฌ๋ฌ ๋จ๋ฝ์ ํฌํจํ ์ ์์ต๋๋ค.</p>
</div>
</section>
"""
var body: some View {
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.padding()
}
}HTML ๋ฌธ์ ๊ตฌ์กฐ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋๋ค. header, main, section, footer ํ๊ทธ๋ฅผ ํ์ฉํฉ๋๋ค.
How to create HTML document structure using header, main, section, and footer tags.
struct DocumentStructureExample: View {
let html = """
<header>
<h1>SwiftUIHTML ๋ฌธ์</h1>
<p>๊ฐ๋ ฅํ HTML ๋ ๋๋ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ</p>
</header>
<main>
<section>
<h2>์ฒซ ๋ฒ์งธ ์น์
</h2>
<p>์ด๊ฒ์ ์ฒซ ๋ฒ์งธ ์น์
์ ๋ด์ฉ์
๋๋ค.</p>
</section>
<section>
<h2>๋ ๋ฒ์งธ ์น์
</h2>
<p>์ด๊ฒ์ ๋ ๋ฒ์งธ ์น์
์ ๋ด์ฉ์
๋๋ค.</p>
<p>์ฌ๋ฌ ๋จ๋ฝ์ ํฌํจํ ์ ์์ต๋๋ค.</p>
</section>
</main>
<footer>
<p>ยฉ 2025 SwiftUIHTML. All rights reserved.</p>
</footer>
"""
var body: some View {
ScrollView {
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.padding()
}
}
}ํ ์คํธ ์ค๋ฐ๊ฟ์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ๋๋ค. br ํ๊ทธ์ ์ค๋ฐ๊ฟ ๋ชจ๋๋ฅผ ์ค์ ํ ์ ์์ต๋๋ค.
How to handle text line breaks. You can use br tags and configure line break modes.
struct LineBreakExample: View {
let html = """
<h2>์ค๋ฐ๊ฟ ์์ </h2>
<p>
์ฒซ ๋ฒ์งธ ์ค์
๋๋ค.<br>
๋ ๋ฒ์งธ ์ค์
๋๋ค.<br>
์ธ ๋ฒ์งธ ์ค์
๋๋ค.
</p>
<p>
๊ธด ํ
์คํธ๋ ์๋์ผ๋ก ์ค๋ฐ๊ฟ๋ฉ๋๋ค.
์ด๊ฒ์ ๋งค์ฐ ๊ธด ๋ฌธ์ฅ์ผ๋ก ํ๋ฉด ๋๋น๋ฅผ ์ด๊ณผํ๋ฉด ์๋์ผ๋ก ๋ค์ ์ค๋ก ๋์ด๊ฐ๋๋ค.
์ค๋ฐ๊ฟ ๋ชจ๋๋ฅผ ์ค์ ํ์ฌ ๋จ์ด ๋จ์ ๋๋ ๋ฌธ์ ๋จ์๋ก ์ค๋ฐ๊ฟ์ ์ ์ดํ ์ ์์ต๋๋ค.
</p>
"""
var body: some View {
VStack(spacing: 20) {
// ๋จ์ด ๋จ์ ์ค๋ฐ๊ฟ
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.htmlEnvironment(\.styleContainer, {
var container = HTMLStyleContainer()
container.lineBreakMode = .byWordWrapping
return container
}())
.border(Color.blue)
// ๋ฌธ์ ๋จ์ ์ค๋ฐ๊ฟ
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.htmlEnvironment(\.styleContainer, {
var container = HTMLStyleContainer()
container.lineBreakMode = .byCharWrapping
return container
}())
.border(Color.green)
}
.padding()
}
}ํฐํธ ํฌ๊ธฐ์ ์คํ์ผ์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋๋ค. UIFont๋ฅผ ์ฌ์ฉํ์ฌ ์์คํ ํฐํธ๋ ์ปค์คํ ํฐํธ๋ฅผ ์ ์ฉํ ์ ์์ต๋๋ค.
How to configure font size and style. You can apply system fonts or custom fonts using UIFont.
struct FontExample: View {
let html = """
<h1>ํฐ ์ ๋ชฉ</h1>
<h2>์ค๊ฐ ์ ๋ชฉ</h2>
<h3>์์ ์ ๋ชฉ</h3>
<p>์ผ๋ฐ ํ
์คํธ์
๋๋ค.</p>
<p style="font-family: 'Helvetica';">Helvetica ํฐํธ๋ฅผ ์ฌ์ฉํ ํ
์คํธ</p>
"""
var body: some View {
VStack(spacing: 20) {
// ๊ธฐ๋ณธ ํฐํธ ํฌ๊ธฐ
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.htmlEnvironment(\.styleContainer, {
var container = HTMLStyleContainer()
container.uiFont = .systemFont(ofSize: 14)
return container
}())
Divider()
// ํฐ ํฐํธ ํฌ๊ธฐ
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.htmlEnvironment(\.styleContainer, {
var container = HTMLStyleContainer()
container.uiFont = .systemFont(ofSize: 18)
return container
}())
}
.padding()
}
}ํ ์คํธ ์ค ๋์ด๋ฅผ ์กฐ์ ํ๋ ๋ฐฉ๋ฒ์ ๋๋ค. ๊ฐ๋ ์ฑ์ ๋์ด๊ธฐ ์ํด ์ค ๊ฐ๊ฒฉ์ ์ค์ ํ ์ ์์ต๋๋ค.
How to adjust text line height. You can set line spacing to improve readability.
struct LineHeightExample: View {
let html = """
<p>
์ด๊ฒ์ ์ฌ๋ฌ ์ค๋ก ๊ตฌ์ฑ๋ ๋จ๋ฝ์
๋๋ค.
์ค ๋์ด๋ฅผ ์กฐ์ ํ๋ฉด ํ
์คํธ ๊ฐ๊ฒฉ์ด ๋ณ๊ฒฝ๋ฉ๋๋ค.
๊ฐ๋
์ฑ์ ๋์ด๊ธฐ ์ํด ์ ์ ํ ์ค ๋์ด๋ฅผ ์ค์ ํ์ธ์.
</p>
"""
var body: some View {
VStack(spacing: 20) {
// ์ข์ ์ค ๊ฐ๊ฒฉ
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.htmlEnvironment(\.styleContainer, {
var container = HTMLStyleContainer()
let font = UIFont.systemFont(ofSize: 14)
container.uiFont = font
container.textLine = .lineHeight(font: font, lineHeight: 18)
return container
}())
.border(Color.red)
// ๋์ ์ค ๊ฐ๊ฒฉ
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.htmlEnvironment(\.styleContainer, {
var container = HTMLStyleContainer()
let font = UIFont.systemFont(ofSize: 14)
container.uiFont = font
container.textLine = .lineHeight(font: font, lineHeight: 28)
return container
}())
.border(Color.blue)
}
.padding()
}
}์ฌ๋ฌ ๊ธฐ๋ฅ์ ํจ๊ป ์ฌ์ฉํ๋ ๋ณตํฉ ์์ ์ ๋๋ค.
A complex example using multiple features together.
struct ComplexExample: View {
let html = """
<header style="background-color: #f0f0f0; padding: 20px;">
<h1 style="color: #333;">SwiftUIHTML ๋ฐ๋ชจ</h1>
<p style="color: #666;">๋ค์ํ ๊ธฐ๋ฅ์ ํ ๋ฒ์ ๋ณด์ฌ์ฃผ๋ ์์ </p>
</header>
<main style="padding: 20px;">
<section>
<h2>ํ
์คํธ ์คํ์ผ</h2>
<p>
<b>๋ณผ๋</b>, <i>์ดํค๋ฆญ</i>, <u>๋ฐ์ค</u>,
<span style="color: red;">๋นจ๊ฐ์</span>,
<span style="background-color: yellow;">๋
ธ๋ ๋ฐฐ๊ฒฝ</span>
</p>
</section>
<section>
<h2>์ด๋ฏธ์ง์ ๋งํฌ</h2>
<p>
<img src="https://picsum.photos/50/50" width="50" height="50" />
์ด๋ฏธ์ง์ <a href="https://github.com">GitHub ๋งํฌ</a>
</p>
</section>
<section style="border: 1px solid #ddd; padding: 10px; border-radius: 5px;">
<h2>์ค์ฒฉ๋ ๋ธ๋ก ์์</h2>
<div>
<p>โข ์ฒซ ๋ฒ์งธ ํญ๋ชฉ</p>
<p>โข ๋ ๋ฒ์งธ ํญ๋ชฉ with <strong>๊ฐ์กฐ</strong></p>
<p>โข ์ธ ๋ฒ์งธ ํญ๋ชฉ</p>
</div>
</section>
</main>
<footer style="background-color: #333; color: white; padding: 10px;">
<p>ยฉ 2025 Example Footer</p>
</footer>
"""
var body: some View {
ScrollView {
HTMLView(html: html, parser: HTMLFuziParser())
.htmlEnvironment(\.configuration, .default)
.htmlEnvironment(\.styleContainer, {
var container = HTMLStyleContainer()
let font = UIFont.systemFont(ofSize: 16)
container.uiFont = font
container.textLine = .lineHeight(font: font, lineHeight: 24)
container.lineBreakMode = .byWordWrapping
return container
}())
.padding()
}
}
}