What's new in TextKit and text views

RSS for tag

Discuss the WWDC22 Session What's new in TextKit and text views

Posts under wwdc2022-10090 tag

11 Posts

Post

Replies

Boosts

Views

Activity

What is the designated way to do custom background drawing in TextKit 2 when using UITextView/NSTextView?
In TextKit 1, I can override drawBackground(forGlyphRange:at:) in NSLayoutManager to do custom background drawing. However, I'm not too sure what the designated way of doing background drawing is in TextKit 2. One thing I've tried is to do custom drawing in my own CALayer that's used in the configureRenderingSurface delegate callback, but I'm unsure if we are suppose to use this API and steal the textViewportLayoutController.delegate away from _UITextLayoutcanvasView?
4
0
3.1k
Jan ’25
Performance Issues with UITextView and TextKit 2
I've been working with UITextView and TextKit 2, which became the default text engine since iOS 16, and I've encountered performance issues when handling very large text documents. Even on iPhone 14 Pro, I've noticed stuttering and frame drops when scrolling through the text view containing a large amount of text. To reproduce the issue, you can use the following code: // In iOS 16 let textView = UITextView() textView.text = "some really large string (say 1 million characters)" The scrolling performance in this scenario is not smooth. However, if you switch to TextKit 1 by accessing the layoutManager property, the performance significantly improves: // In iOS 16 let textView = UITextView() let _ = textView.layoutManager // this enables the compatibility mode textView.text = "some really large string (say 1 million characters)" With this code, scrolling remains smooth even with large text documents. It's very disappointing to see TextKit 2 performing worse than TextKit 1, even though Apple claims TextKit 2 has significantly improved performance with noncontiguous layout. Furthermore, the sample code for TextKit 2, available here, crashes when handling very large text due to excessive memory usage. Has anyone else experienced similar performance issues with TextKit 2, and are there any potential solutions or workarounds to improve the performance?
2
0
2.1k
Apr ’24
How can I automatically insert the next NSTextList list item on return in iOS 16?
I'm adding some text formatting support to an editable text view in my app, and I want to include bulleted lists. I specifically want to have the standard behavior where if you're typing a line of text in a list and press Enter, the next bullet item is automatically inserted. This appears to somewhat work out-of-the-box with NSTextList on iOS 16, unless the line you're editing is at the very end of the document. This is apparent in the sample code for WWDC 2022 session 10090 (project is here): Run that sample app and switch to the List tab Put your cursor at the end of any line except the last one and press enter. You get a new list item, as expected. Put your cursor at the end of the very last line and press Enter. You don't get a list item until you type something on that line. I've likewise found that if I have this text in a text view: Hello World\n\nList here\n If I format the text List here as a bulleted list (by creating an NSMutableParagraphStyle with its textLists property set and adding it to the attributed string for that range) and press Enter at the end of the word Here, I get a new bullet item automatically. But if I do the same thing without having that last newline after Here, the new list item is not inserted. How can I make the list auto-continuation behavior work at the end of the document?
1
0
1k
Apr ’23
TextKit2 Multi Column Layout
I'm trying to implement a multi column layout for a hex editor using TextKit2. I'm using the sample App named "LayoutTextWithTextKit2" as my starting point, but I'm struggling to understand what the best approach could be to implement it. My doubts are: Is it a good idea to use a text element for a single line in a single column? I'm basing my text element on NSTextParagraph. Following my previous question, is there any way to layout these text elements horizontally in a single line, instead of having the layout engine automatically lay them out in a vertical stack? Where is the correct location where I can override the positioning for each text element manually if I wanted to not base my elements in NSTextParagraph? Would this be a good idea? I apologize for the many questions, but my basic problem is that it's not clear to me if I'm missing something that's obvious, and there's a simple way to address my troubles. Any guidance towards that goal, even beyond my questions above, will be appreciated. I'm attaching an image showing two fragments that I'd like to be laid out horizontally in the same line, for reference.
0
0
1.2k
Sep ’22
How does CoreText's CTFontManager deal with variable UIFont?
Suppose I make a UIFont like this: let variations = myCustomVariationsDictionary     let key = kCTFontVariationAttribute as UIFontDescriptor.AttributeName     let uiFontDescriptor = UIFontDescriptor(fontAttributes: [.name: Self.name, key: variations])     let updatedUIFont = UIFont(descriptor: uiFontDescriptor, size: uiFont.pointSize) self.uiFont = updatedUIFont —thereby creating a variable font in RAM and saving it to some object. How do I then register that created font in the CTFontManager such that we can access it by name from SwiftUI? The variation name doesn't correspond with any font file on disk; it will be something crazy like "RobotoFlex_wght0BADF00D_YXCD8282384729" (or whatever) and will be different based the values used for each of the variation axes. The problem I'm facing is that you have to specify the font size when you create a given variable font, or UIFont instance. Now, that font is baked in at that particular size, which might not be the appropriate optical size for the final size it might scale to as a result of DynamicType (e.g. as with an @ScaledMetric for the size). But we can't have the wrapper around the CTFont methods use @ScaledFont property wrapper, since this wrapper does nothing unless used on a conformance to View that's actively being used in an actual SwiftUI graph. In other words, it seems like we have to dynamically recreate the font every single time we want to use it in a Text view, which causes all kinds of nasty memory leaks and unbounded growth of RAM not to mention horrible performance. I've looked at various options available in github—there's exactly one SwiftUI wrapper around dynamic fonts, and it's very uninspiring. So here I am, asking you lot for any advice. Really, I prolly need to file a DTS ticket, but figured I'd try here first. Thanks!
0
0
0
Aug ’22
UITextView + TextKit 2: Are we allowed to configure our own custom rendering surface when using UITextView?
Hi TextKit 2 Team, I'm trying to configure my own custom rendering surface when using UITextView(usingTextLayoutManager: true). What I'm currently doing is adding a "contentLayer: CALayer" into my UITextView and adding my rendering surfaces into that. The issue I'm running into, is that the rendering surface will display the first character I input, but then will not update for subsequent text until: It becomes long enough to cause a line wrap. I hit return. Not sure if I'm doing something wrong? So my questions are: Is using configureRenderingSurface API intended to be used when I'm using UITextView? Or it's only meant for when I do fully custom InputView? Should I be inserting my rendering surfaces into my contentLayer or is there some more appropriate place for me to insert into? How do I properly trigger redraw or invalidation of my rendering surface on text editing changes? Is this a bug or do I have to trigger an update manually somewhere? Many thanks in advance!
0
0
1.4k
Jun ’22
Is it possible to use UITextView in #TextKit2 with a subclass of NSTextContentManager without using NSTextStorage as backing store?
When I tried this, the app crashed immediately with a requirement to use NSTextContentStorage subclass
Replies
4
Boosts
0
Views
1.9k
Activity
Oct ’25
What is the designated way to do custom background drawing in TextKit 2 when using UITextView/NSTextView?
In TextKit 1, I can override drawBackground(forGlyphRange:at:) in NSLayoutManager to do custom background drawing. However, I'm not too sure what the designated way of doing background drawing is in TextKit 2. One thing I've tried is to do custom drawing in my own CALayer that's used in the configureRenderingSurface delegate callback, but I'm unsure if we are suppose to use this API and steal the textViewportLayoutController.delegate away from _UITextLayoutcanvasView?
Replies
4
Boosts
0
Views
3.1k
Activity
Jan ’25
Performance Issues with UITextView and TextKit 2
I've been working with UITextView and TextKit 2, which became the default text engine since iOS 16, and I've encountered performance issues when handling very large text documents. Even on iPhone 14 Pro, I've noticed stuttering and frame drops when scrolling through the text view containing a large amount of text. To reproduce the issue, you can use the following code: // In iOS 16 let textView = UITextView() textView.text = "some really large string (say 1 million characters)" The scrolling performance in this scenario is not smooth. However, if you switch to TextKit 1 by accessing the layoutManager property, the performance significantly improves: // In iOS 16 let textView = UITextView() let _ = textView.layoutManager // this enables the compatibility mode textView.text = "some really large string (say 1 million characters)" With this code, scrolling remains smooth even with large text documents. It's very disappointing to see TextKit 2 performing worse than TextKit 1, even though Apple claims TextKit 2 has significantly improved performance with noncontiguous layout. Furthermore, the sample code for TextKit 2, available here, crashes when handling very large text due to excessive memory usage. Has anyone else experienced similar performance issues with TextKit 2, and are there any potential solutions or workarounds to improve the performance?
Replies
2
Boosts
0
Views
2.1k
Activity
Apr ’24
How can I automatically insert the next NSTextList list item on return in iOS 16?
I'm adding some text formatting support to an editable text view in my app, and I want to include bulleted lists. I specifically want to have the standard behavior where if you're typing a line of text in a list and press Enter, the next bullet item is automatically inserted. This appears to somewhat work out-of-the-box with NSTextList on iOS 16, unless the line you're editing is at the very end of the document. This is apparent in the sample code for WWDC 2022 session 10090 (project is here): Run that sample app and switch to the List tab Put your cursor at the end of any line except the last one and press enter. You get a new list item, as expected. Put your cursor at the end of the very last line and press Enter. You don't get a list item until you type something on that line. I've likewise found that if I have this text in a text view: Hello World\n\nList here\n If I format the text List here as a bulleted list (by creating an NSMutableParagraphStyle with its textLists property set and adding it to the attributed string for that range) and press Enter at the end of the word Here, I get a new bullet item automatically. But if I do the same thing without having that last newline after Here, the new list item is not inserted. How can I make the list auto-continuation behavior work at the end of the document?
Replies
1
Boosts
0
Views
1k
Activity
Apr ’23
TextKit2 Multi Column Layout
I'm trying to implement a multi column layout for a hex editor using TextKit2. I'm using the sample App named "LayoutTextWithTextKit2" as my starting point, but I'm struggling to understand what the best approach could be to implement it. My doubts are: Is it a good idea to use a text element for a single line in a single column? I'm basing my text element on NSTextParagraph. Following my previous question, is there any way to layout these text elements horizontally in a single line, instead of having the layout engine automatically lay them out in a vertical stack? Where is the correct location where I can override the positioning for each text element manually if I wanted to not base my elements in NSTextParagraph? Would this be a good idea? I apologize for the many questions, but my basic problem is that it's not clear to me if I'm missing something that's obvious, and there's a simple way to address my troubles. Any guidance towards that goal, even beyond my questions above, will be appreciated. I'm attaching an image showing two fragments that I'd like to be laid out horizontally in the same line, for reference.
Replies
0
Boosts
0
Views
1.2k
Activity
Sep ’22
How does CoreText's CTFontManager deal with variable UIFont?
Suppose I make a UIFont like this: let variations = myCustomVariationsDictionary     let key = kCTFontVariationAttribute as UIFontDescriptor.AttributeName     let uiFontDescriptor = UIFontDescriptor(fontAttributes: [.name: Self.name, key: variations])     let updatedUIFont = UIFont(descriptor: uiFontDescriptor, size: uiFont.pointSize) self.uiFont = updatedUIFont —thereby creating a variable font in RAM and saving it to some object. How do I then register that created font in the CTFontManager such that we can access it by name from SwiftUI? The variation name doesn't correspond with any font file on disk; it will be something crazy like "RobotoFlex_wght0BADF00D_YXCD8282384729" (or whatever) and will be different based the values used for each of the variation axes. The problem I'm facing is that you have to specify the font size when you create a given variable font, or UIFont instance. Now, that font is baked in at that particular size, which might not be the appropriate optical size for the final size it might scale to as a result of DynamicType (e.g. as with an @ScaledMetric for the size). But we can't have the wrapper around the CTFont methods use @ScaledFont property wrapper, since this wrapper does nothing unless used on a conformance to View that's actively being used in an actual SwiftUI graph. In other words, it seems like we have to dynamically recreate the font every single time we want to use it in a Text view, which causes all kinds of nasty memory leaks and unbounded growth of RAM not to mention horrible performance. I've looked at various options available in github—there's exactly one SwiftUI wrapper around dynamic fonts, and it's very uninspiring. So here I am, asking you lot for any advice. Really, I prolly need to file a DTS ticket, but figured I'd try here first. Thanks!
Replies
0
Boosts
0
Views
0
Activity
Aug ’22
UITextView + TextKit 2: Are we allowed to configure our own custom rendering surface when using UITextView?
Hi TextKit 2 Team, I'm trying to configure my own custom rendering surface when using UITextView(usingTextLayoutManager: true). What I'm currently doing is adding a "contentLayer: CALayer" into my UITextView and adding my rendering surfaces into that. The issue I'm running into, is that the rendering surface will display the first character I input, but then will not update for subsequent text until: It becomes long enough to cause a line wrap. I hit return. Not sure if I'm doing something wrong? So my questions are: Is using configureRenderingSurface API intended to be used when I'm using UITextView? Or it's only meant for when I do fully custom InputView? Should I be inserting my rendering surfaces into my contentLayer or is there some more appropriate place for me to insert into? How do I properly trigger redraw or invalidation of my rendering surface on text editing changes? Is this a bug or do I have to trigger an update manually somewhere? Many thanks in advance!
Replies
0
Boosts
0
Views
1.4k
Activity
Jun ’22
How to access the textContentStorage of UITextView in iOS 16?
Hi TextKit team, How do I access the textContentStorage of UITextView that is usingTextLayoutManager: true? I'd like to use the NSTextContentStorageDelegate methods, but unlike NSTextView where it has a property for textContentStorage, it seems UITextView doesn't expose this? Thanks for your help in advance! Cheers / Leo
Replies
1
Boosts
0
Views
1.3k
Activity
Jun ’22
Where is the TextKitAndTextView sample code?
Hi TextKit2 Team, I'm trying to find the link to download the TextKitAndTextView sample code that was mentioned in the "What's new in TextKit and text views" video. I can't seem to find a link to it anywhere though. Cheers / Leo
Replies
1
Boosts
0
Views
1.2k
Activity
Jun ’22
Is TextKit 2 built on Core Text?
It's a simple question. I'm just trying to understand how these things fit together. Core Text doesn't seem to get updated much, but maybe that's because it's low level and doesn't need it. I assume it's still the recommended way to do low-level text things not handled by TextKit. ?
Replies
1
Boosts
0
Views
1.6k
Activity
Jun ’22
Where is the sample code for TextKitAndTextViewSampleApp?
I just watched the WWDC22 video: "What's new in Textkit and text views" but don't see a resource link for the sample code for TextKitAndTextViewSampleApp. Does anyone have the link for this sample code?
Replies
1
Boosts
2
Views
952
Activity
Jun ’22