RichTextKit is a Swift-based library for working with rich text in UIKit, AppKit and SwiftUI.

Overview

RichTextKit Logo

Version Swift 5.6 MIT License Twitter: @danielsaidi

About RichTextKit

RichTextKit is a Swift-based library that lets you work with rich text in UIKit, AppKit and SwiftUI.

RichTextKit is under development. Currently missing parts will be added over time.

RichTextKit is supported by and released with permission from Oribi and used in OribiWriter, which is out on iOS and soon on macOS.

Supported Platforms

RichTextKit supports iOS 14, macOS 12, tvOS 14 and watchOS 8.

Installation

RichTextKit can be installed with the Swift Package Manager:

https://github.com/danielsaidi/RichTextKit.git

or with CocoaPods:

pod RichTextKit

Getting started

To get started with RichTextKit, have a look at this getting started guide.

Documentation

The online documentation contains more information, code examples etc. and makes it easy to overview the various parts of the library.

Demo Application

This project contains a demo app that lets you explore RichTextKit on iOS and macOS. To run it, just open and run Demo/Demo.xcodeproj.

Support

You can sponsor this project on GitHub Sponsors or get in touch for paid support.

Contact

Feel free to reach out if you have questions or if you want to contribute in any way:

License

RichTextKit is available under the MIT license. See the LICENSE file for more info.

Comments
  • Storing to core data

    Storing to core data

    A hint for those who need to store the text to core data: set the field to "Transformable".

    The vars are declared like:

    @State var information = NSAttributedString(string: "")
    init() {
    _information = State(initialValue: Entity.Attribute as! NSAttributedString)
    }
    
    discussion 
    opened by karlmira 12
  • Menus for Adding Text at Cursor Position

    Menus for Adding Text at Cursor Position

    As this cool RTE is OpenSource, I'd like to share a small peace of code for a nice additional feature.

    Background: I have to write many letters and very often I have to add the same piece of text, that I call "snippet". Therefor add an extra Menu (e.g. to the TopToolBar)

    Menu {
    	ForEach(snippets, id: \.self) { snippet in
    		Button {
    			context.pasteText(snippet.text!, at: context.selectedRange.location, moveCursorToPastedContent: true)
    		} label: {
    			Text(snippet.title!)
    		}
    	}
    } label: {
    	Text("insertSnippet")
    }
    .help("help_selectMarker")
    

    Snippets here: a FetchedResults from Core Date

    Have fun.

    discussion 
    opened by karlmira 6
  • Publishing changes from within view updates is not allowed, this will cause undefined behavior.

    Publishing changes from within view updates is not allowed, this will cause undefined behavior.

    Hi @danielsaidi, Just found several warning as below

    Warning Publishing changes from within view updates is not allowed, this will cause undefined behavior. in function func syncContextWithTextView() in RichTextCoordinator

    bug 
    opened by joel-meng 6
  • How to set `standardRichTextFontSize`?

    How to set `standardRichTextFontSize`?

    How do I set the standardRichTextFontSize for my editor? The package file itself is not editable.

    RichTextEditor(text: $textBodyAttrib, context: context) {
    	$0.setCurrentFontSize(to: 11)
    }
    

    is not working properly and is overwritten.

    discussion 
    opened by karlmira 4
  • Keyboard toolbar modifier doesn't work

    Keyboard toolbar modifier doesn't work

    Since RichTextKit wraps UIKit and AppKit, the SwiftUI toolbar view modifier with keyboard placement doesn't work.

    In other words, this will not work:

    RichTextEditor(text: $text, context: textEditorContext)
        .frame(minHeight: 200)
        .toolbar {
            ToolbarItem(placement: .keyboard) {
                Button {
                    richTextContext.isBold.toggle()
                } label: {
                    Image(systemName: "bold")
                }
            }
        }
    

    In my own app, I developed a custom toolbar that was pinned above the keyboard. The library should offer this custom toolbar as a feature.

    feature 
    opened by danielsaidi 3
  • Wrap RichTextCoordinator.syncContextWithTextView() in DispatchQueue

    Wrap RichTextCoordinator.syncContextWithTextView() in DispatchQueue

    image Xcode 14 produces runtime warnings for this function, as it updates the UI from a background thread. This PR wraps the function in `DispatchQueue.main.async`. An alternative would be to use `@MainActor`.

    This PR should get tested before merging!

    opened by chFlorian 2
  • subscribeToContextChanges is not subscribing to subscribeToShouldPasteImage and subscribeToShouldPasteImages

    subscribeToContextChanges is not subscribing to subscribeToShouldPasteImage and subscribeToShouldPasteImages

    In RichTextCoordinator the subscribeToContextChanges is not calling the functions subscribeToShouldPasteImage and subscribeToShouldPasteImages. Because of that, images are not showing on the text editor.

    bug 
    opened by MarcosChevis 2
  • Add support for focus values

    Add support for focus values

    To properly add support for macOS and iPadOS menu commands, we need focus values.

    Make the rich text editor bind its context to a focus value, then use this context in the menu commands.

    feature 
    opened by danielsaidi 1
  • Add support for keyboard shortcuts

    Add support for keyboard shortcuts

    The various buttons should have opt-out keyboard shortcuts applies, for instance:

    • [x] Style buttons and toggles - cmd+b, cmd+I, cmd+u
    • [x] Action buttons and toggles - cmd+c, cmd+v, cmd+z, cmd+shift+z
    feature 
    opened by danielsaidi 1
  • Add support for strikethrough

    Add support for strikethrough

    It should be possible to apply a strikethrough style.

    • [x] Add support to the views, context, coordinator etc.
    • [x] Add standard icon
    • [x] Add it to the demo
    feature 
    opened by danielsaidi 1
  • Some styles are not reset when moving the cursor on macOS

    Some styles are not reset when moving the cursor on macOS

    On macOS, the background and foreground colors aren't reset when moving the text input cursor from colored text to a plain text.

    image

    In the image above, all text was plain. I then made the leftmost text green. When I then moved the cursor to the end of the text and continued typing, the green color was still applied.

    bug 
    opened by danielsaidi 1
  • Changing text position redraws entire app

    Changing text position redraws entire app

    This is related to https://github.com/danielsaidi/RichTextKit/issues/5

    While investigating, I added a random color to parts of the demo app. It turns out that the entire app redraws every time the text position is moved:

    image image

    This is because the RichTextCoordinator syncs the text field's selectedRange to the RichTextContext when the text position changes, which happens when you move the cursor or type in the text field.

    This is horrible for performance, but I don't know how to fix it. The rich text context is synced in full with the text field, which means that views that take the context as observed object will redraw if the range changes, even if they don't use the range.

    Any input to help me fix this problem would be greatly appreciated.

    bug 
    opened by danielsaidi 1
  • Foreground color is not persisted

    Foreground color is not persisted

    If you set a text color with the current implementation, the text view is properly updated. You can try it out in the demo app.

    However, if you save the rich text to file or write its data to NSUserDefaults and then restores the rich text, everything that is currently supported by the library (font, font size, background color, style etc.) is restored, except the text color.

    The text color is handled exactly like the background color, except that it uses the foregroundColor key instead of the backgroundColor, so I just can't understand why this single thing doesn't work.

    bug help-wanted 
    opened by danielsaidi 0
  • macOS sometimes ignores font size

    macOS sometimes ignores font size

    Sometimes, when placing the cursor in a text block that has a certain font size, starting to type will cause the font size to go back to the default font.

    bug 
    opened by danielsaidi 0
  • Add support for links

    Add support for links

    It should be possible to add a link to the current selection, and remote the link if there is already one.

    • [ ] Add support for adding a link to the the current text position.
    • [ ] Add support for removing a link from the current text position.
    • [ ] Add support for detecting a link at the current text position.
    • [ ] Add standard icon
    • [ ] Add it to the demo
    feature 
    opened by danielsaidi 0
  • Add support for letter spacing

    Add support for letter spacing

    It should be possible to adjust the letter spacing.

    When implementing, do this:

    • [ ] Add support to the views, context, coordinator etc.
    • [ ] Add standard icon
    • [ ] Add it to the demo
    feature 
    opened by danielsaidi 0
  • Add support for line height

    Add support for line height

    It should be possible to adjust the line height.

    • [ ] Add support to the views, context, coordinator etc.
    • [ ] Add standard icon
    • [ ] Add it to the demo
    feature 
    opened by danielsaidi 0
Releases(0.4.0)
  • 0.4.0(Dec 20, 2022)

    This release addresses some performance changes, by trying to minimize the number of redraws.

    As a result, the RichTextContext's selectedRange is no longer observable, since that caused every input or text position change to redraw the entire app. The library and demo app however still updates way to often, and too much. For instance, switching between having a selected range and not should only redraw the copy button, but now updates the entire screen. If you know how to minimize this, please reach out.

    Furthermore, this release adds support for focus values and menu commands. You can see them in action in the demo app.

    ✨ New Features

    • Commands is a new namespace for app commands.
    • Focus is a new namespace for focus values.
    • RichTextDataFormat has a new fileFormatText property.
    • RichTextDataFormat has a new isArchivedDataFormat property.
    • RichTextDataFormat has a new libraryFormats property that returns all formats except the vendorArchivedData format.
    • RichTextDataFormatMenu is a new menu that triggers an action for various formats.
    • RichTextDataReader now has ways to get data for the current rich text.
    • RichTextExportMenu is a new menu for exporting rich text.
    • RichTextContextFocusedValueKey is a new rich text context focus key.
    • RichTextFormatCommandMenu is a new command menu for adding rich text formatting to the system menu.
    • RichTextNSSharingMenu is a new macOS-specific menu for sharing rich text with NSSharingServices.
    • RichTextShareCommandMenu is a new command menu for adding rich text sharing and exporting to the system menu.
    • RichTextShareMenu is a new menu for sharing rich text.
    • ViewDebug is a new demo app class that is used for view debugging capabilities.
    • The library is now localized in Danish, German and Norwegian, although some translations probably need adjusting.

    πŸ’‘ Behavior changes

    • RichTextCoordinator now checks if properties have changed before it syncs.

    πŸ› Bug Fixes

    • RichTextKeyboardToolbar no longer allocates width for hidden items.
    • RichTextStyleToggle is no longer tinted by default when inactive.

    πŸ—‘οΈ Deprecated

    • PdfDataReader has been renamed to RichTextPdfDataReader.
    • RichTextDataWriter is deprecated.
    • RichTextDataWriter functionality has been moved to RichTextDataReader.

    πŸ’₯ Breaking Changes

    • RichTextContext no longer has a font size in the initializer, use StandardFontSizeProvider (CGFloat, TextEditor etc.) instead.
    • RichTextContext selectedRange is no longer published.
    • RichTextDataReader now implements RichTextReader.
    • RichTextDataReader format-specific data functions have been made private.
    • RichTextStyleToggle.Style inactiveColor is now optional.
    • NSMutableString format-specific initializers have been made private.
    Source code(tar.gz)
    Source code(zip)
  • 0.3.0(Dec 15, 2022)

    This release adds support for strikethrough and cleans up some 0.2 code. It also adds a bunch of new views, such as toolbars, sidebars, sheets etc., as well as support for keyboard shortcuts, localization and accessibility.

    ✨ New Features

    • Bundle.richTextKit is a new bundle extension that works in external previews.
    • RichTextAction has a new dismissKeyboard action.
    • RichTextAction has new incrementFontSize and decrementFontSize actions.
    • RichTextAction has new undo and redo name aliases.
    • RichTextActionButton has a new fillVertically parameter.
    • RichTextFormatSheet is a new view that collects a bunch of text formatting controls.
    • RichTextFormatSidebar is a new view that collects a bunch of text formatting controls.
    • RichTextKeyboardToolbar is a new toolbar that can be used on iOS.
    • RichTextKeyboardToolbarMenu is a new toolbar menu.
    • RichTextStyle has a new strikethrough style.
    • RichTextStyleToggle has a new fillVertically parameter.
    • RTKL10n is a new enum with localized strings, which is used to localize multiple types.
    • View+KeyboardShortcuts is a new view extension to simplify binding keyboard shortcuts to views.
    • The various stack views now support

    πŸ’‘ Behavior changes

    • RichTextActionButtonStack and RichTextStyleToggleStack now fills vertically.
    • RichTextStyleButton and RichTextStyleToggle now applies keyboard shortcuts.
    • RichTextStyleToggle applies a toggle-like style to fallbacks on older OS versions.
    • Many pickers, buttons and toggles apply a localized accessibility label.
    • More buttons apply a rectangular content shape.

    πŸ’₯ Breaking Changes

    • RichTextActionButtonStack no longer has default actions.
    • RichTextActionButtonStack no longer has a bordered init parameter.
    • RichTextAlignmentPicker no longer has title and segmented parameters.
    • RichTextFontSizePickerStack no longer has a bordered init parameter.
    • RichTextFontSizePickerStack now requires a RichTextContext instea dof a binding.
    Source code(tar.gz)
    Source code(zip)
  • 0.2.0(Dec 10, 2022)

    ✨ New Features

    • Image has new rich text-specific images.
    • NSImage cgImage and jpegData are now public.
    • NSAttributedString has a new withBlackText() extension.
    • NSAttributedString has a new init extension file.
    • RichTextAction is a new enum that defines rich text actions.
    • RichTextActionButton is a new view that can trigger a RichTextAction.
    • RichTextActionButtonStack is a new view that can list multiple RichTextActionButton views.
    • RichTextContext has new bindings.
    • RichTextCoordinator now subscribes to highlighting style changes.
    • RichTextCoordinator cancellables are now public.
    • RichTextCoordinator resetHighlightedRangeAppearance() is now public.
    • RichTextCoordinator text is now mutable.
    • RichTextColorPicker is a new color picker.
    • RichTextColorPickerStack is a new view that can list multiple RichTextColorPicker views.
    • RichTextDataFormat has a new vendor-specific data format.
    • RichTextFontSizePickerStack is a new view that can list multiple RichTextFontSizePicker views.
    • RichTextImageAttachment is now open for inheritance.
    • RichTextStyleButton has a new button style.
    • RichTextStyleToggle is a new style toggle button.
    • RichTextStyleToggleStack is a new view that can list multiple RichTextStyleToggle views.
    • RichTextView is now open for inheritance.
    • RichTextView drop interaction functionality is now open.
    • String extensions have been made public.

    πŸ’‘ Behavior changes

    • RichTextAlignmentPicker is now segmented by default.

    πŸ—‘οΈ Deprecated

    • Font picker components have been renamed with a RichText prefix.
    • FontSizePicker sizes has been renamed to values.
    • PdfDataWriter has been renamed to PdfDataReader.
    • RichTextAlignmentPicker alignments has been renamed to values.
    • RichTextContext alignment has been renamed to textAlignment.
    • RichTextContext standardHighlightingStyle has been renamed to highlightingStyle.
    • RichTextCoordinator context has been renamed to richTextContext.
    • RichTextDataWriter richTextData(with:) has been renamed to richTextData(for:).
    • RichTextViewRepresentable has been renamed to RichTextViewComponent.

    πŸ’₯ Breaking Changes

    • RichTextView alert function title parameter is no longer implicit.
    • RichTextViewRepresentable decrementFontSize has been renamed to decrementCurrentFontSize and has no range parameter.
    • RichTextViewRepresentable incrementFontSize has been renamed to incrementCurrentFontSize and has no range parameter.
    Source code(tar.gz)
    Source code(zip)
  • 0.1.2(Nov 1, 2022)

  • 0.1.1(Jul 23, 2022)

  • 0.1.0(Jun 6, 2022)

    This is the first beta release of RichTextKit. πŸš€

    The release includes the following.

    ✨ Foundational Types

    • RichTextView is a replacement for UITextView and NSTextView.
    • RichTextEditor is a SwiftUI view that embeds a RichTextView.
    • RichTextContext is used to inspect and interact with a RichTextEditor.
    • RichTextCoordinator is used by a RichTextEditor to keep the text view and context in sync.

    ✨ Feature support

    RichTextKit adds extensive support for a bunch of rich text features:

    • Alignment
    • Attributes
    • Colors
    • Data
    • Export
    • Fonts
    • Images
    • Pasteboard extensions
    • Pdf
    • Sharing
    • Styles
    • Views
    Source code(tar.gz)
    Source code(zip)
Owner
Daniel Saidi
Freelance software engineer, focusing on mobile products and Apple platforms like iOS, tvOS and watchOS.
Daniel Saidi
SwiftUI TextEdit View - A proof-of-concept text edit component in SwiftUI & CoreText.

A proof-of-concept text edit component in SwiftUI & CoreText. No UIKit, No AppKit, no UITextView/NSTextView/UITextField involved.

Marcin Krzyzanowski 80 Dec 1, 2022
AEOTPTextField - A beautiful iOS OTP Text Field library, written in Swift with full access customization in UI.

AEOTPTextField - A beautiful iOS OTP Text Field library, written in Swift with full access customization in UI.

Abdelrhman Kamal 79 Jan 3, 2023
Focus text field in SwiftUI dynamically and progress through form using iOS keyboard.

Focuser Focuser allows to focus SwiftUI text fields dynamically and implements ability move go through the form using Keyboard for iOS 13 and iOS 14.

Art Technologies 118 Dec 25, 2022
A SwiftUI TextField with a prompt (or placeholder) that floats above the text field when active or not empty. Requires iOS 15.

FloatingPromptTextField A prompt is the label in a text field that informs the user about the kind of content the text field expects. In a default Tex

Emilio PelΓ‘ez 43 Nov 3, 2022
Render Markdown text in SwiftUI

MarkdownUI MarkdownUI is a library for rendering Markdown in SwiftUI, fully compliant with the CommonMark Spec. Supported Platforms You can use the Ma

Guille Gonzalez 916 Jan 8, 2023
Autocomplete for a text field in SwiftUI using async/await

Autocomplete for a text field in SwiftUI using async/await

Dmytro Anokhin 13 Oct 21, 2022
ExpandableText 😎 (SwiftUI) Expand the text with the "more" button

ExpandableText ?? (SwiftUI) Expand the text with the "more" button Get Started import SwiftUI import ExpandableText struct ExpandableText_Test: View

null 35 Dec 26, 2022
Transition from any SwiftUI Text view into an inline navigation bar title when the view is scrolled off-screen, as seen in Apple's TV & TestFlight iOS apps.

SwiftUI Matched Inline Title Transition from any SwiftUI Text view into an inline navigation bar title when the view is scrolled off-screen, as seen i

Seb Jachec 19 Oct 9, 2022
Render Markdown text in SwiftUI

Render Markdown text in SwiftUI. It is a preview based on the Marked implementation

ε°εΌŸθ°ƒθ°ƒβ„’ 26 Dec 20, 2022
A beautiful and flexible text field control implementation of "Float Label Pattern". Written in Swift.

SkyFloatingLabelTextField SkyFloatingLabelTextField is a beautiful, flexible and customizable implementation of the space saving "Float Label Pattern"

Skyscanner 4k Jan 1, 2023
Changes the color of the label text when the button is pressed and also prints hello to the console

MY FIRST APP App Description This app changes the color of the label text when the button is pressed and also prints "hello" to the console. App Walk-

null 0 Nov 29, 2021
UITextField and UITextView subclasses with placeholders that change into floating labels when the fields are populated with text.

Deprecated Please use JVFloatLabeledTextField instead or feel free to chime in on an issue if you'd like to take over the repo. RPFloatingPlaceholders

rob phillips 1.1k Jan 5, 2023
A text view that supports selection and expansion

The Problem UILabel and UITextView offer unsatisfying support for text selection. Existing solutions like TTTAttributedLabel are great but offer a som

Jeff Hurray 636 Dec 20, 2022
Declarative text styles and streamlined Dynamic Type support for iOS

StyledText StyledText is a library that simplifies styling dynamic text in iOS applications. Instead of having to use attributed strings every time yo

Blue Apron 233 Oct 18, 2022
Lightweight set of text fields with nice animation and functionality

TweeTextField This is lightweight library that provides different types of Text Fields based on your needs. I was inspired by Jan Henneberg. Features

Oleg 471 Nov 10, 2022
Handles some of the base configuration to make creating a UIAlertController with text entry even easier. Plus validation!

?? FancyTextEntryController A simpler/easier API for adding text fields to UIAlertControllers. Not a custom view, just uses good ol' UIAlertController

Christian Selig 22 Jul 18, 2022
ARAutocompleteTextView is a subclass of UITextView that automatically displays text suggestions in real-time

ARAutocompleteTextView is a subclass of UITextView that automatically displays text suggestions in real-time. This is perfect for automatically suggesting the domain as a user types an email address, #hashtag or @alexruperez.

Alex RupΓ©rez 261 Jun 29, 2022
UITextField that automatically formats text to display in the currency format

CurrencyTextField The numbers that the user enters in the field are automatically formatted to display in the dollar amount format. For example, if th

Richa Deshmukh 49 Sep 28, 2022
An auto growing text input bar for messaging apps.

ALTextInputBar An auto growing text input bar for messaging apps. Written in Swift. ALTextInputBar is designed to solve a few issues that folks usuall

Alex Littlejohn 265 Nov 15, 2022