Skip to content

Latest commit

ย 

History

History
411 lines (345 loc) ยท 13.1 KB

File metadata and controls

411 lines (345 loc) ยท 13.1 KB

๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ• / Basic Usage

SwiftUIHTML์˜ ๊ธฐ๋ณธ์ ์ธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ฉ๋‹ˆ๋‹ค. This guide introduces the basic usage of SwiftUIHTML.

1. ๊ฐ„๋‹จํ•œ HTML ๋ Œ๋”๋ง / Simple HTML Rendering

ํ•œ๊ธ€ ์„ค๋ช…

๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ HTML ๋ Œ๋”๋ง ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค. HTMLView๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํŒŒ์„œ๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

English

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)
    }
}

2. ํ…์ŠคํŠธ ์Šคํƒ€์ผ๋ง / Text Styling

ํ•œ๊ธ€ ์„ค๋ช…

๋‹ค์–‘ํ•œ ํ…์ŠคํŠธ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ณผ๋“œ, ์ดํƒค๋ฆญ, ์–ธ๋”๋ผ์ธ ๋“ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

English

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
            }())
    }
}

3. ์ด๋ฏธ์ง€ ์‚ฝ์ž… / Image Embedding

ํ•œ๊ธ€ ์„ค๋ช…

HTML ๋‚ด์— ์ด๋ฏธ์ง€๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์›น URL์ด๋‚˜ ๋กœ์ปฌ ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋‘ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

English

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()
        }
    }
}

4. ๋งํฌ ์ฒ˜๋ฆฌ / Link Handling

ํ•œ๊ธ€ ์„ค๋ช…

ํด๋ฆญ ๊ฐ€๋Šฅํ•œ ๋งํฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋งํฌ ํƒœ๊ทธ๋Š” ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

English

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)
    }
}

5. ๋ธ”๋ก ์š”์†Œ ๊ตฌ์กฐ / Block Element Structure

ํ•œ๊ธ€ ์„ค๋ช…

๋‹ค์–‘ํ•œ ๋ธ”๋ก ์š”์†Œ๋“ค์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. div, p, section ๋“ฑ์˜ ๊ธฐ๋ณธ ๋ธ”๋ก ํƒœ๊ทธ๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.

English

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()
    }
}

6. ํ—ค๋”์™€ ์„น์…˜ ๊ตฌ์กฐ / Headers and Section Structure

ํ•œ๊ธ€ ์„ค๋ช…

HTML ๋ฌธ์„œ ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. header, main, section, footer ํƒœ๊ทธ๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.

English

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()
        }
    }
}

7. ์ค„๋ฐ”๊ฟˆ ์ฒ˜๋ฆฌ / Line Break Handling

ํ•œ๊ธ€ ์„ค๋ช…

ํ…์ŠคํŠธ ์ค„๋ฐ”๊ฟˆ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. br ํƒœ๊ทธ์™€ ์ค„๋ฐ”๊ฟˆ ๋ชจ๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

English

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()
    }
}

8. ํฐํŠธ ์„ค์ • / Font Configuration

ํ•œ๊ธ€ ์„ค๋ช…

ํฐํŠธ ํฌ๊ธฐ์™€ ์Šคํƒ€์ผ์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. UIFont๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์Šคํ…œ ํฐํŠธ๋‚˜ ์ปค์Šคํ…€ ํฐํŠธ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

English

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()
    }
}

9. ์ค„ ๋†’์ด ์„ค์ • / Line Height Configuration

ํ•œ๊ธ€ ์„ค๋ช…

ํ…์ŠคํŠธ ์ค„ ๋†’์ด๋ฅผ ์กฐ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ์ค„ ๊ฐ„๊ฒฉ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

English

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()
    }
}

10. ๋ณตํ•ฉ ์˜ˆ์ œ / Complex Example

ํ•œ๊ธ€ ์„ค๋ช…

์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๋ณตํ•ฉ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

English

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()
        }
    }
}