Meet TextKit 2

RSS for tag

Discuss the WWDC21 session Meet TextKit 2.

Posts under wwdc21-10061 tag

10 Posts

Post

Replies

Boosts

Views

Activity

Can NSTextView work with custom NSTextContentManager implementation? (TextKit2)
I'm trying to implement custom NSTextContentManager and use it with NSTextView, however it seems that NSTextView expect NSTextContentStorage all the time. final class MyTextContentManager: NSTextContentManager { // ... } It's added to layout manager, and NSTextView instance finds it properly: let textContentManager = MyTextContentManager() textContentManager.addTextLayoutManager(textLayoutManager) however, when I use it, I see errors at: [MyTextContentManager textStorage]: unrecognized selector sent to instance 0x600003d84870 the textStorage property is part of NSTextStorageObserving, that is not NSTextContentManager interface. It looks like NSTextView is not ready to work with custom NSTextContentManager. What did I miss?
3
0
3.4k
Jun ’24
TextKit2: Relation between NSRange and NSTextRange
The way NSTextView is built it's inevitable to use NSTextStorage with TextKit2, however the NSAttributedString uses NSRange vs the TextKit2 family uses NSTextRange for text location, etc. What I struggle with is the relation between these two. I didn't find a convenient translation between these two. Is NSAttributedStrint NSRange length=1 equal to NSTextRange offset 1? I think it's not (at least it's not necessarily true for every NSTextContentManager. So my question is, given a NSTextRange, what is the corresponding NSRange in NSTextContentStorage.attributedString
4
0
2.9k
Mar ’24
Page-based layouts with TextKit 2?
In the "old" TextKit, page-based layout is accomplished by providing an array of NSTextContainers to NSLayoutManager, each with its own NSTextView. TextKit 2, NSTextLayoutManager allows only a single text container. Additionally, NSTextParagraph seems to be the only concrete NSTextElement class. Paragraphs often need to break across page boundaries. How would one implement page-based layout in TextKit 2?
4
0
4k
Aug ’23
When does viewport bounds gets recalculated?
The demo app has a problem: if you vertically shrink the window using the bottom edge and move it to position y0, then drag the bottom edge again to another position y1, parts of the document after y0 will not be rendered due to the fact the viewport bounds are not recalculated when frame height changes. My fix to this problem is to remove the check that inspects whether the text container width changed and always set the value of the text container size used by the text layout manager whenever frame size changes: this does fix the problem mentioned above. However, I would like to know exactly when will the text layout manager recalculate the viewport bounds. This is not documented anywhere, nor in the video.
1
0
1.5k
Jun ’23
tracksTextAttachmentViewBounds not working
Hello :), I want to have a UIView inside of a UITextView. For that I use the new NSTextAttachmentViewProvider class introduced in iOS 15. The views width should always be equal to the width of the UITextView this width should update when, for example, the screen rotates. To do that I am using the tracksTextAttachmentViewBounds property inside of a NSTextAttachmentViewProvider subclass. If I understand correctly, if this property is set to true, the function attachmentBounds(for:location:textContainer:proposedLineFragment:position:) of my NSTextAttachmentViewProvider subclass should be used to determine the views bounds. In the code example below I have set it up in that manner, sadly the function is never called. (The storyboard consists of a UIViewController with a UITextView which four constrains (trailing, leading, bottom, top) are set equal to the safe area, nothing special going on). I have also tried to use a NSTextAttachment subclass in which I override the attachmentBounds(for:location:textContainer:proposedLineFragment:position:) function. It is also not called. The view is appearing, but not with the width and height that I have set in the function (see screenshot below), maybe it is using some default values. When I start typing, the view disappears. I don't know what I am doing wrong. Could somebody help me with that problem? import UIKit class SomeNSTextAttachmentViewProvider : NSTextAttachmentViewProvider { override func loadView() { super.loadView() tracksTextAttachmentViewBounds = true view = UIView() view!.backgroundColor = .purple } override func attachmentBounds( for attributes: [NSAttributedString.Key : Any], location: NSTextLocation, textContainer: NSTextContainer?, proposedLineFragment: CGRect, position: CGPoint ) -> CGRect { return CGRect(x: 0, y: 0, width: proposedLineFragment.width, height: 200) } } class ViewController: UIViewController { @IBOutlet var textView: UITextView? override func viewDidLoad() { super.viewDidLoad() NSTextAttachment.registerViewProviderClass(SomeNSTextAttachmentViewProvider.self, forFileType: "public.data") let mutableAttributedString = NSMutableAttributedString() mutableAttributedString.append(NSAttributedString("purple box: ")) mutableAttributedString.append( NSAttributedString( attachment: NSTextAttachment(data: nil, ofType: "public.data") ) ) textView?.attributedText = mutableAttributedString textView?.font = UIFont.preferredFont(forTextStyle: .body) } }
1
0
1.9k
Jun ’22
How Text Layout Manager generates Layout Fragment from Text Elements?
During the presentation (19:43) it says the NSTextLayoutFragment 'Layout information for one or more elements'. But from the documentation it seems that you can only generate a layout fragment from one NSTextElement. Besides there is only one property that associates a text element. What does it mean by 'parent element' anyway?
0
0
1k
Apr ’22
TextKit 2 Demo Question
Hi there, I have a small question regards to the TextKit 2 demo app: In macOS / TextDocumentView.swift / viewportBounds(for:) / Line 106 , why compare overdrawRect.minY with max(visibleRect.minY, 0)? Isn’t overdrawRect.minY always the smaller one (as it’s an overdraw rect)? Another question would be, if I'm planning to start working on an application with complex text editing features, would you recommend using NSTextView for now and wait for further updates, or just use the TextKit 2 stack (with CALayers) like the one used in the demo? I really love TextKit 2 and so far the demo seems pretty performant. But I don't know if there is any drawbacks if I build a TextKit 2 stack on my own so I'm looking forward to your advice. Many thanks
0
0
1.1k
Dec ’21
Proper way to customize layout fragments' frames?
I found that compared with TextKit 1, NSTextLayoutManager does not provide any opportunity to customize line rectangles. In particular, I cannot do much about the frame of a NSTextLayoutFragment. I am curious about the proper approach to: limit the width and horizontal offset of a paragraph; add spacing below and above a paragraph; enclose a paragraph along an arbitrary path. For the first question, I noticed that in the sample app, when layout comments, BubbleLayoutFragment does not change the positions of any characters. While it did override renderingSurfaceBounds, it merely expands the bounds, without moving its text contents. And there seems to be no way to do so. For the second question, I noticed that paragraphSpacing and paragraphSpacingBefore set on the paragraph style seems to have no effect. According to the video, all layout fragments are stacked like a list. Therefore, this is an intended feature, right? Right now, my solutions to the first two questions are: If I want to control the horizontal position of a paragraph, I could simply set headIndent and trailIndent on the paragraph style. If I want to add spacing above or below a paragraph, I can add empty lines and set minimalLineHeight for the newline character. Are those the recommended ways to achieve such effect? Personally, I am not very interested in the third feature, enclosing a paragraph along an arbitrary path. However, I am indeed curious about how to add paragraph-level attachment, such as an block image/custom view between paragraphs. I think what I could do is: add an empty new line and set its height to leave enough space for the custom view; configure a CALayer or even a view and put it where the new line should be; If I want to layout characters within a strange shape, I could compute the shape's height based on the width, using TextKit 1, and then insert the new line as in step 1. Is this the intended way to approach such situation?
2
0
1.8k
Nov ’21
Can NSTextView work with custom NSTextContentManager implementation? (TextKit2)
I'm trying to implement custom NSTextContentManager and use it with NSTextView, however it seems that NSTextView expect NSTextContentStorage all the time. final class MyTextContentManager: NSTextContentManager { // ... } It's added to layout manager, and NSTextView instance finds it properly: let textContentManager = MyTextContentManager() textContentManager.addTextLayoutManager(textLayoutManager) however, when I use it, I see errors at: [MyTextContentManager textStorage]: unrecognized selector sent to instance 0x600003d84870 the textStorage property is part of NSTextStorageObserving, that is not NSTextContentManager interface. It looks like NSTextView is not ready to work with custom NSTextContentManager. What did I miss?
Replies
3
Boosts
0
Views
3.4k
Activity
Jun ’24
TextKit2: Relation between NSRange and NSTextRange
The way NSTextView is built it's inevitable to use NSTextStorage with TextKit2, however the NSAttributedString uses NSRange vs the TextKit2 family uses NSTextRange for text location, etc. What I struggle with is the relation between these two. I didn't find a convenient translation between these two. Is NSAttributedStrint NSRange length=1 equal to NSTextRange offset 1? I think it's not (at least it's not necessarily true for every NSTextContentManager. So my question is, given a NSTextRange, what is the corresponding NSRange in NSTextContentStorage.attributedString
Replies
4
Boosts
0
Views
2.9k
Activity
Mar ’24
Page-based layouts with TextKit 2?
In the "old" TextKit, page-based layout is accomplished by providing an array of NSTextContainers to NSLayoutManager, each with its own NSTextView. TextKit 2, NSTextLayoutManager allows only a single text container. Additionally, NSTextParagraph seems to be the only concrete NSTextElement class. Paragraphs often need to break across page boundaries. How would one implement page-based layout in TextKit 2?
Replies
4
Boosts
0
Views
4k
Activity
Aug ’23
When does viewport bounds gets recalculated?
The demo app has a problem: if you vertically shrink the window using the bottom edge and move it to position y0, then drag the bottom edge again to another position y1, parts of the document after y0 will not be rendered due to the fact the viewport bounds are not recalculated when frame height changes. My fix to this problem is to remove the check that inspects whether the text container width changed and always set the value of the text container size used by the text layout manager whenever frame size changes: this does fix the problem mentioned above. However, I would like to know exactly when will the text layout manager recalculate the viewport bounds. This is not documented anywhere, nor in the video.
Replies
1
Boosts
0
Views
1.5k
Activity
Jun ’23
tracksTextAttachmentViewBounds not working
Hello :), I want to have a UIView inside of a UITextView. For that I use the new NSTextAttachmentViewProvider class introduced in iOS 15. The views width should always be equal to the width of the UITextView this width should update when, for example, the screen rotates. To do that I am using the tracksTextAttachmentViewBounds property inside of a NSTextAttachmentViewProvider subclass. If I understand correctly, if this property is set to true, the function attachmentBounds(for:location:textContainer:proposedLineFragment:position:) of my NSTextAttachmentViewProvider subclass should be used to determine the views bounds. In the code example below I have set it up in that manner, sadly the function is never called. (The storyboard consists of a UIViewController with a UITextView which four constrains (trailing, leading, bottom, top) are set equal to the safe area, nothing special going on). I have also tried to use a NSTextAttachment subclass in which I override the attachmentBounds(for:location:textContainer:proposedLineFragment:position:) function. It is also not called. The view is appearing, but not with the width and height that I have set in the function (see screenshot below), maybe it is using some default values. When I start typing, the view disappears. I don't know what I am doing wrong. Could somebody help me with that problem? import UIKit class SomeNSTextAttachmentViewProvider : NSTextAttachmentViewProvider { override func loadView() { super.loadView() tracksTextAttachmentViewBounds = true view = UIView() view!.backgroundColor = .purple } override func attachmentBounds( for attributes: [NSAttributedString.Key : Any], location: NSTextLocation, textContainer: NSTextContainer?, proposedLineFragment: CGRect, position: CGPoint ) -> CGRect { return CGRect(x: 0, y: 0, width: proposedLineFragment.width, height: 200) } } class ViewController: UIViewController { @IBOutlet var textView: UITextView? override func viewDidLoad() { super.viewDidLoad() NSTextAttachment.registerViewProviderClass(SomeNSTextAttachmentViewProvider.self, forFileType: "public.data") let mutableAttributedString = NSMutableAttributedString() mutableAttributedString.append(NSAttributedString("purple box: ")) mutableAttributedString.append( NSAttributedString( attachment: NSTextAttachment(data: nil, ofType: "public.data") ) ) textView?.attributedText = mutableAttributedString textView?.font = UIFont.preferredFont(forTextStyle: .body) } }
Replies
1
Boosts
0
Views
1.9k
Activity
Jun ’22
How Text Layout Manager generates Layout Fragment from Text Elements?
During the presentation (19:43) it says the NSTextLayoutFragment 'Layout information for one or more elements'. But from the documentation it seems that you can only generate a layout fragment from one NSTextElement. Besides there is only one property that associates a text element. What does it mean by 'parent element' anyway?
Replies
0
Boosts
0
Views
1k
Activity
Apr ’22
TextKit 2 Demo Question
Hi there, I have a small question regards to the TextKit 2 demo app: In macOS / TextDocumentView.swift / viewportBounds(for:) / Line 106 , why compare overdrawRect.minY with max(visibleRect.minY, 0)? Isn’t overdrawRect.minY always the smaller one (as it’s an overdraw rect)? Another question would be, if I'm planning to start working on an application with complex text editing features, would you recommend using NSTextView for now and wait for further updates, or just use the TextKit 2 stack (with CALayers) like the one used in the demo? I really love TextKit 2 and so far the demo seems pretty performant. But I don't know if there is any drawbacks if I build a TextKit 2 stack on my own so I'm looking forward to your advice. Many thanks
Replies
0
Boosts
0
Views
1.1k
Activity
Dec ’21
Proper way to customize layout fragments' frames?
I found that compared with TextKit 1, NSTextLayoutManager does not provide any opportunity to customize line rectangles. In particular, I cannot do much about the frame of a NSTextLayoutFragment. I am curious about the proper approach to: limit the width and horizontal offset of a paragraph; add spacing below and above a paragraph; enclose a paragraph along an arbitrary path. For the first question, I noticed that in the sample app, when layout comments, BubbleLayoutFragment does not change the positions of any characters. While it did override renderingSurfaceBounds, it merely expands the bounds, without moving its text contents. And there seems to be no way to do so. For the second question, I noticed that paragraphSpacing and paragraphSpacingBefore set on the paragraph style seems to have no effect. According to the video, all layout fragments are stacked like a list. Therefore, this is an intended feature, right? Right now, my solutions to the first two questions are: If I want to control the horizontal position of a paragraph, I could simply set headIndent and trailIndent on the paragraph style. If I want to add spacing above or below a paragraph, I can add empty lines and set minimalLineHeight for the newline character. Are those the recommended ways to achieve such effect? Personally, I am not very interested in the third feature, enclosing a paragraph along an arbitrary path. However, I am indeed curious about how to add paragraph-level attachment, such as an block image/custom view between paragraphs. I think what I could do is: add an empty new line and set its height to leave enough space for the custom view; configure a CALayer or even a view and put it where the new line should be; If I want to layout characters within a strange shape, I could compute the shape's height based on the width, using TextKit 1, and then insert the new line as in step 1. Is this the intended way to approach such situation?
Replies
2
Boosts
0
Views
1.8k
Activity
Nov ’21