Infomaniak 富文本 HTML 编辑器

Infomaniak 富文本 HTML 编辑器 是一个强大的 Swift 包,旨在为 iOS、macOS 和 visionOS 平台提供无缝的 WYSIWYG(所见即所得)文本编辑体验。利用 contenteditable HTML 属性的强大功能,此编辑器允许您轻松编辑 HTML 内容。

它使用 WebKit 构建,支持 UIKitAppKit,并且还包含一个 SwiftUI 端口,可以轻松集成到现代 Swift 应用程序中。

iOS sample app iOS sample app iOS sample app

正在寻找 Android 版本的编辑器?请查看 Kotlin 版本的编辑器:Infomaniak/android-rich-html-editor

✍️ 关于

特性

安装

您可以通过 Swift Package Manager 安装此包。将以下行添加到您的 Package.swift 文件中

.package(url: "https://github.com/Infomaniak/swift-rich-html-editor.git", from: "1.0.0")

用法

UIKit 和 AppKit

您可以创建编辑器视图,然后将其添加到视图层级结构中。

import InfomaniakRichHTMLEditor
import UIKit

let editor = RichHTMLEditorView()
view.addSubview(editor)

要响应编辑器的事件,您可以遵循 RichHTMLEditorViewDelegate 协议。

SwiftUI

SwiftUI 视图称为 RichHTMLEditor,它接受两个参数

import InfomaniakRichHTMLEditor
import SwiftUI

struct ContentView: View {
    @State private var html = ""
    @StateObject private var textAttributes = TextAttributes()

    var body: some View {
        RichHTMLEditor(html: $html, textAttributes: textAttributes)
    }
}

TextAttributes 对象包含有关所选文本的当前样式的各种属性。这些属性是只读的,并由编辑器自动更新。要更新样式,您应该调用相应的函数,例如 bold()

提供了许多修饰符来自定义编辑器并响应编辑器的事件。这是一个非详尽的修饰符列表

RichHTMLEditor(html: $html, textAttributes: textAttributes)
    .editorScrollable(true)
    .editorInputAccessoryView(myToolbarView)
    .editorCSS("h1 { foreground-color: red; }")
    .onEditorLoaded {
        // Perform action when editor is loaded
    }
    .onCaretPositionChange { newPosition in
        // Perform action when caret moves
    }
    .onJavaScriptFunctionFail { error, function in
        // Perform action when an editor JavaScript function has failed
    }
    .introspectEditor { richEditorView in
        // Perform action on the editor (UI|NS)View
    }

自定义编辑器

您可以使用 CSS 自定义编辑器。要定位编辑器,您应该使用 #swift-rich-html-editor 选择器。

例如

#swift-rich-html-editor {
    padding: 16px;
}

📖 文档

已记录公共类型,并且提供了三个示例项目来帮助您实现编辑器。

🔍 示例项目

您可以在 Examples 文件夹中找到 3 个示例项目

📱 使用 InfomaniakRichHTMLEditor 的应用程序

Find App

Infomaniak Mail 允许您在完全安全的环境中管理您的 Infomaniak 地址。