TextKit 2: is it reliable?

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

1 Like

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

2 Likes

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

1 Like

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.

3 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.)

1 Like

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.

7 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

1 Like

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.

1 Like

Where did you see that Apple is using TextKit 2 in Notes app? I’d expect that to be using a custom text system based on CoreText - they tend not to use TextKit in their own apps outside of TextEdit. I’d be surprised if it was using TextKit 2 rather than a bespoke text system.

Also, I should have been more clear: when I said that tables don’t work in TextKit 2, I’m talking about NSTextTable, the table support that is built into the text system (although only on macOS). You can of course build your own tables code from scratch, but that is a lot of work (you’d need to write your own code for drawing the tables, interacting with them, saving and loading them to and from file, and so on).

The tables in Notes are not TextKit tables, but custom tables code written by Apple that is not available for developers to use. I’m not sure TextKit 2 is mature enough yet even to write your own tables code, though, which is in part why I’d be very surprised in Notes is built using it. Certainly you would not be able to write it in such a way that you could print anything, since TK2 doesn’t support text being broken across multiple pages.

1 Like

okay thank you for the information :heart:
do you know any fundamental problems that we might face, if we try and build our own custom implementations on top of TextKit 2.

1 Like

With due respect, isn’t there all kinds of possible “fundamental problems” already suggested up-thread as current short-comings, if you need all those things that are already given as shortcomings of TextKit 2? It is what it is. At this point, perhaps best to try it and see how you get on?

3 Likes

We have already started with our approach, we are looking if there is something about textkit2 that would make it impossible to work, something that can’t be handle by us, Example, text selection across tables and paragraphs, apples notes app selects the whole table, but we are aiming at selecting only the text content till the cursor, we are trying to implement our workarounds but would tk2 let us ?

1 Like

Perhaps seek an Apple Developer’s Forum which focuses on TextKit for further advice and counsel.

3 Likes

@KB any thoughts on this ?

1 Like

What makes you think that Notes uses TextKit 2?

It may be an AI hallucination.

E.g.:

But if you click on the sources used, they don’t mention it at all - it’s just blindly confusing the word “notes” (as in “minutes”) with the app name.

2 Likes

It’s a very strange phenomenon that folk are using LLMs as fact machines. i mean, I know folk have the most bizarre abstractions in their heads about life, the universe, and everything, but those things are complex, and understanding that an LLM is not a fact machine is trivial.

3 Likes

we have conducted few tests
test1-
tk2 layouts the text as fragments which makes it perform better than tk1, but if there are thousands of lines in a single paragraph (if you never hit return after a sentence ), all the text is layed out in a single fragment which affects the performance, the textview in tk2 lags, even while typing, we have noticed the same behavior In apples notes app, but tk1 renders it smoothly

tk1 - Watch TextKit1-test1 | Streamable
tk2 - Watch TextKit2-test1 | Streamable
notes app - Watch NotesApp-test1 | Streamable

test2-
when you separate the text into fragments (hit return after a couple of sentences), tk2 performs very smoothly even for thousands of lines, same is the case for apples notes app, tk1 performs same as before,
if we keep increasing the text content tk1 performance degrades but not of tk2 and notes app, also selecting texts as a block of objects and dropping them is a feature of Tk2 which is available in notes app as well

tk2- Watch TextKit2-test2 | Streamable
notes app - Watch NotesApp-test2 | Streamable

1 Like

@KB what do you think of this ??