RichTextKit is a library that lets you view and edit rich text in Swift and SwiftUI, on all major Apple platforms.
The RichTextEditor
supports text styles (bold, italic, underline, etc.), fonts, font sizes, colors, text alignments, image attachments, and much more. It's powered by a RichTextView
that bridges UITextView
& NSTextView
and adds additional, platform-agnostic APIs that make the two views behave more alike.
If you just want to view rich text content, you can use the RichTextViewer
SwiftUI view, which wraps the editor and applies a read-only configuration to it.
RichTextKit can be installed with the Swift Package Manager:
https://github.com/danielsaidi/RichTextKit.git
You can become a sponsor to help me dedicate more time on my various open-source tools. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed.
RichTextKit has a SwiftUI RichTextEditor
that takes a text binding and a RichTextContext
:
struct MyView: View {
@State
private var text = NSAttributedString(string: "Type here...")
@StateObject
var context = RichTextContext()
var body: some View {
RichTextEditor(text: $text, context: context) {
// You can customize the native text view here
}
.focusedValue(\.richTextContext, context)
}
}
The editor uses a RichTextCoordinator
to sync changes between the editor, context, and platform-specific view. You can use the context to change font, colors, alignment etc. and observe context changes to update the UI.
If you just want to display rich text, you can use the RichTextViewer
instead:
struct MyView: View {
private var text = NSAttributedString(...)
var body: some View {
RichTextViewer(text: text)
}
}
RichTextKit provides UI components, keyboard shortcuts & menu commands that can be used in a rich text editor.
For more information, please see the getting started guide.
The online documentation has more information, articles, code examples, etc.
The RichTextKit demo lets you explore the library on iOS & macOS. To try it out, just open and run the Demo
app.
RichTextKit is also used in the following apps, so make sure to check them out for inspiration:
Don't hesitate to reach out if you are using RichTextKit, and want to add your app to this list. I'd love to feature it.
Feel free to reach out if you have questions or want to contribute in any way:
- Website: danielsaidi.com
- E-mail: [email protected]
- Bluesky: @[email protected]
- Mastodon: @[email protected]
RichTextKit is available under the MIT license. See the LICENSE file for more info.