SwiftUI, WebKit with editor.js is an option I have explored.
Example code:
import SwiftUI
import WebKit
struct EditorJSView: UIViewRepresentable {
@State private var paragraphContent = "Hello World. Using SwiftUI and Webkit with Editor.js to Build RichText Editor under SwiftUI"
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
webView.navigationDelegate = context.coordinator
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
let editorJSURL = URL(string: "https://cdn.jsdelivr.net/npm/@editorjs/editorjs@2.18.0/dist/editor.js")!
let script = """
var editor = new EditorJS({
holder: 'editorjs',
data: {
"blocks": [
{
"type": "paragraph",
"data": {
"text": "\(paragraphContent)",
}
}
]
}
});
"""
let fullHTML = """
<html>
<head>
<script src="\(editorJSURL.absoluteString)"></script>
</head>
<body style="color:blue;font-size: 100px;">
<div id="editorjs" contenteditable="true"></div>
<script>
\(script)
</script>
</body>
</html>
"""
uiView.loadHTMLString(fullHTML, baseURL: nil)
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, WKNavigationDelegate {
var parent: EditorJSView
init(_ parent: EditorJSView) {
self.parent = parent
}
}
}
struct ContentView1: View {
var body: some View {
VStack {
EditorJSView()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView1()
}
}