TextKit 2: is it reliable?

@KB Do you think textKit 2 Is reliable in 2024 for integrating in iOS/macOS ?

TextKit 2 is definitely reliable. It is used system-wide, including in Scrivener, for text fields (the small fields where you enter numbers or small chunks of info).

TextKit 2 is not yet ready for editing documents, though. You could use it for a plain ext editor or a coding app as long as you don’t require printing, but not for anything more complex. At the time of writing (November 2024), TextKit 2 is missing the following main features:

  • Support for multiple containers and thus multiple pages. This means that it cannot be used for a page view such as Scrivener’s View > Text Editing > Show Page Layout option. It also means that it cannot be used for printing documents, since you need to allow for printing multiple pages when printing text, even if the editor doesn’t have a page view.
  • Tables.

There are other missing features too, but, off the top of my head, these are the main two that prevent us from adopting it in the main writing areas of our apps.

All the best,
Keith

1 Like

So using textKit2 as a text editor is not a good option, right ?
my requirements are making lists, nested lists, changing indents, font styles, sizes
@KB

Support for lists were added to TextKit 2 last year, so you might be able to do what you want with it. You can certainly change font styles and sizes, indents and so on with no problem. You will, however, have a problem if you want to print the text - for that you’ll need to use TextKit 1.

TextEdit is Apple’s TextKit showcase, and it uses TextKit 2 by default. However, if you insert a table, or if you choose Format > Wrap to Page, it switches to TextKit 1. It also uses TextKit 1 for printing. But when you aren’t using tables, page view, or printing, you can see from TextEdit that a lot is possible in TextKit 2.

If you’re starting a new app, it’s definitely best to use TextKit 2 if you can, because it will be the future. It’s just a shame that Apple has been so slow in adding all of the features needed for word processing.

2 Likes

Is there anything that’s better compared to v1? What’s Apples’s goal moving forward? (They likely won’t abandon or extend RTF.)

The RTF part is all the same. Basically, TextKit 1 has the following main elements:

  • Text Storage: the underlying data - the actual text itself.
  • Layout Manager: an object that manages laying out the text storage. One text storage can be placed in multiple layout managers, which is how you can view the same text in different areas.
  • Text Container : a box that the text is laid out inside. The layout manager lays out the text in the container. One layout manager can have multiple text containers, in which case the text is laid out across them - this is how you get multiple pages. (The code has to handle adding and removing containers based on the amount of text there is.)
  • Text View: this is the UI element that displays a single text container and allows you to edit the text inside it. (You don’t have to use this for printing.)

TextKit 2 has the following main elements:

  • Text Storage - this is the same as TextKit 1. The text storage is used for reading and writing files, so none of that is affected.
  • Text ContentStorage - a wrapper around the text storage object which has an apparently more modern way of working with ranges of text (read: more complicated, as far as I’m concerned at least).
  • Text Layout Manager: again, an object that lays out the text storage. But now, rather than managing laying out the text directly, it does so through multiple “Text Layout Fragments”, which are supposed to make it easier to customize text layout. In practice, though, this can cause problems, because it’s now much harder to adjust the layout or appearance of a section of text based on other text around it, because the code you are working with only knows about fragments of text rather than the whole of it.
  • Text Container: the same as TextKit 1. However, a TK2 Text Layout Manager can currently only be associated with a single container, which is why page layout (and thus printing) is not yet possible.
  • Text Kit: same as TK1.

So the underlying text and the containers and UI view are all the same, but the stuff in the middle is all different.

There is some pretty cool stuff that TK2 can do that is not possible in TK1, such as folding sections of text (as long as they are whole paragraphs), but none of it is particularly useful for our apps. And some of the changes will make some of the things we do harder.

When TextKit 2 was introduced four years (I think) ago, Apple made it quite clear that it was the future and that apps should adopt it as soon as possible over TK1. Progress has been glacial, though, and as far as I can see, there were absolutely no TK2 enhancements in the 2024 updates, presumably because they diverted a lot of manpower to Apple Intelligence and the integration of that throughout both TextKits.

Which means the text system has been in a state of limbo for the past few years: TextKit 2 is the future, but it’s not yet usable for most writing apps. To the extent that although I was very familiar with TextKit 2 a year or so ago, and had written a bunch of code ready for when we have to make the switch (although there is far more to write), I’d have to remind myself of the basics again now, since I haven’t had any reason to touch it in months.

6 Likes

@KB what do you feel are the pros and cons of TextKit 2 over 1, if I start my app in TextKit 2 what are the actual benefits I will get currently over textkit1, rather than making the app in textkit1 and migrating to textkit2 when it is stable and better

I think I’ve already covered the cons - TextKit 2 cannot be used for tables, printing, or page layout. It’s also more difficult to do custom drawing that has to span paragraph boundaries.

The pros are really only that it is more modern so when Apple adds new stuff, they focus on TextKit 2. For instance, Writing Tools, when used inside the text view (rather than in a panel), have fancy animations in TextKit 2 but not in TextKit 1. (This stuff has been added before lots of the basics in TextKit 2, but there you go.)

Text folding is also possible in TextKit 2 but not in TextKit 1 (where you can hide sections of text), but unfortunately this seems to be limited to folding only paragraphs, not arbitrary ranges of text.

The decision as to which to use will depend on what your app needs to do. If you can use TextKit 2, you should use it, simply because that is the direction Apple is heading, and at some point down the road they will no doubt stop updating TK1 and eventually deprecate it.

But if you need any of the things that TK2 can’t do (printing, page layout, tables), then you’ll have no choice but to use TK1.

1 Like

Thankyou for your responses, We discovered that Apple is using textkit 2 in their Notes app. They are supporting tables but selection across other text areas and between text in table cells doesn’t seem to work. It selects the entire table, all or nothing.
It does seem possible to come up with work arounds. But with serious compromises. In your experience is there anything else except this text selection issue that’s fundamentally as problematic in TextKit 2.