RichTextKit lets you edit rich text in UIKit, AppKit, and SwiftUI. It has a multi-platform RichTextView
and a SwiftUI RichTextEditor
that supports changing style (bold, italic, underline etc.), font, font sizes, colors, text alignment, etc. You can even drag in and paste images if you use a text format that allows it.
RichTextKit is supported by and released with permission from Oribi and used in OribiWriter, which is out on iOS and soon on macOS. Have a look at that app or the demo app in this repo if you want to see RichTextKit in action.
RichTextKit can be installed with the Swift Package Manager:
https://github.com/danielsaidi/RichTextKit.git
or with CocoaPods:
pod RichTextKit
If you prefer to not have external dependencies, you can also just copy the source code into your app.
RichTextKit supports iOS 14
, macOS 11
, tvOS 14
and watchOS 7
.
The online documentation has a getting started guide to help you get started with RichTextKit.
In UIKit and AppKit, you can start with creating RichTextView
view instead of a UITextView
or NSTextView
:
RichTextView(data: myData, format: .archivedData) // Using data
RichTextView(string: myString, format: .plainText) // Using a string
RichTextView
has a lot more functionality than the native views and bridges the platform-specific api:s so that the views behave more alike across platforms. You can use this view to both view and edit rich text.
In SwiftUI, you can use a RichTextEditor
, which connects a wrapped RichTextView
with 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
}
}
}
The RichTextEditor
uses a coordinator to sync changes between the context and the editor. You can now use the context to change font, font size, colors, alignment etc. and observe how these properties change when you move the cursor around the text view.
For more information, please see the online documentation and getting started guide.
The online documentation contains more information, code examples, etc., and makes it easy to overview the various parts of the library.
The demo app lets you explore the library on iOS and macOS. To try it out, just open and run the Demo
project.
You can sponsor this project on GitHub Sponsors or get in touch for paid support.
Feel free to reach out if you have questions or if you want to contribute in any way:
- Website: danielsaidi.com
- Mastodon: @[email protected]
- Twitter: @danielsaidi
- E-mail: [email protected]
RichTextKit is available under the MIT license. See the LICENSE file for more info.