Manage text storage and perform custom layout of text-based content in your app's views using TextKit.

Posts under TextKit tag

176 Posts

Post

Replies

Boosts

Views

Activity

NSLayoutManager.drawGlyphs() can not display MarkDown attributed string
We use NSLayoutManager/NSTextStorage/NSTextContainer to display markdown attributed string on a canvas. MarkDown string was processed properly, but NSLayoutManager just exhibits the plain text. The code is as follows:     var layoutmanager = NSLayoutManager()     var textstorage = NSTextStorage()     var textcontainer = NSTextContainer()   var attrStr = try AttributedString.init(markdown: "**test**", options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace)) var attrString = NSMutableAttributedString(attrStr)  self.textstorage.setAttributedString(attrString!) // draw text layout     let range = self.layoutmanager.glyphRange(for: self.textcontainer)     self.layoutmanager.drawBackground(forGlyphRange: range, at: self.textLocation)     self.layoutmanager.drawGlyphs(forGlyphRange: range, at: self.textLocation) Is it because TextKit 1 does not support markdown string display or I miss something else ? Any help will be appreciated. Thanks!
1
0
1.5k
Nov ’21
Adding un-selectable text to NSTextParagraph?
I want to add characters to the display of my paragraphs without modifying the underlying text storage. To be concrete, let's say that I want to prefix each paragraph with "Whereas, ". Further, when I drag out a text selection on a paragraph that's displayed this way, I do not want "Whereas, " to receive a selection highlight. Using TextKit 2, it's pretty easy to accomplish the first part using a NSTextContentStorageDelegate whose method textContentStorage(_: NSTextContentStorage, textParagraphWith: NSRange) -> NSTextParagraph? adds my prefix to each paragraph. I modified the WWDC21 demo program to do it. I appreciate how I can avoid glyph munging by using TextKit 2 instead of TextKit 1, it will save a lot of trouble. If I drag a selection across the prefix, then it receives the selection highlight. What's more, when I drag a selection across the end of the paragraph, the last eight characters (and, I presume, the invisible newline) do not receive the selection highlight. So adding the prefix has skewed the selection indices. It seems to me that the TextKit 2 designers may intend for me to use a NSTextLocation subclass consisting of a "storage" index and a "display" sub-index. On the storage indices there are the characters of the text storage, and at (storage index, display sub-index)-pairs are the characters of my prefix. While the user drags out a selection, I should subtract from the NSTextSelection any NSTextRanges belonging to non-storage text such as my prefix. Am I on the right track, so far?
0
0
924
Nov ’21
Multipage Layout of Text on iOS and MacOS with TextKit 2
Hi! First apologies if I may have missed this in the forums somewhere, but I have been unable to find any documentation on multi-page layouts with the newer text kit and have not been able to create this using/modifying the sample code provided from the WWDC conference earlier this year. What I am trying to do is create a multipage document where the text is editable and the layout is fairly static. When the text expands in the current view to where there is no room left, a new 'page' will be created and the text will spill over there. Basically standard word or google docs type editor. The position for the pages will need be manipulated depending on some properties. I also intend in wrapping the solution in SwiftUI since the rest of the app this will be used in is a SwiftUI app. If this isn't possible, I may just stick with TextKit1 or punt altogether. Thanks! Sean
2
0
1.2k
Nov ’21
TextKit2: Strange behaviour in directional text selection navigation
Hi there, I am currently trying to extend the TextKit 2 demo project from WWDC 2021 to allow keyboard input under iOS using UITextInteraction. For the most part, it is working well, but the behaviour of the directional cursor text navigation is strange: When moving up or down, it keeps the horizontal location within the current paragraph. But when crossing an empty line into another paragraph, the former horizontal position is lost and the cursor is reset to the line start. This is a video showing the behaviour: https://www.icloud.com/iclouddrive/0Xwf9hpb2DcAoU1CNPZnVgwQw#TextKit2_Selection_Bug I would expect the cursor to restore its former horizontal position in following paragraphs. This is the standard behaviour in UITextView and NSTextView. Does anybody know if this a bug in TextKit2 or if there is a way to achieve the expected beahviours. Thanks Frank
1
0
1.4k
Nov ’21
Custom paste into UITextView
I am implementing a UITextView subclass that supports the editing of structured text with block styles (e.g. paragraph, heading, list, quote etc.) and inline styles (e.g. emphasis, code etc.) I need to be able to cut, copy and paste structured text from the UITextView. Implementing this is straightforward: override cut(_:), copy(_:) and paste(_:) and read/write the structured text (a JSON format) to/from the general pasteboard. This works fine, but the UITextView's smart insert/delete behavior (see UITextInputTraits.smartInsertDeleteType), which adjusts spaces around the selection, is lost. I have explored implementing a custom text paste delegate (UITextPasteDelegate). This would appear to provide support for custom paste behaviour. I can to extract the contents of the pasteboard and transform them to an attributed string for pasting by implementing textPasteConfigurationSupporting(_:, transform:) however any custom attributes I add to the string placed in the UITextPasteItem are ignored (standard attributes are also ignored because allowsEditingTextAttributes is set to false). I can solve this issue by implementing textPasteConfigurationSupporting(_:, performPasteOf:, to:) but this breaks the smart insert/delete behaviour, even when pasting plain text. Unfortunately I can see no way to paste custom textual data into a UITextView without losing the standard smart insert/delete behaviour. Am I missing something? Any pointers on how I can achieve cut/copy/paste for a custom clipboard format while maintaining smart insert/delete would be appreciated.
0
0
1.6k
Oct ’21
SF Pro in Figma
Hello, I have previous versions on SF fonts installed on my Mac, including SF Pro Display. I recently downloaded the updated SF Pro, but it does not show up in Figma. I can see it and use it fine in Sketch. Does anyone know how to resolve this issue so I can access it in Figma?
3
0
11k
Sep ’21
TextKit 2 and SwiftUI
Is it possible to use/draw a SwiftUI view in TextKit 2? The NSTextLayoutManagerDelegate seems to be built around returning a NSTextLayoutFragment. Since NSTextLayoutFragment is a class, I can't subclass it and use SwiftUI. I'd love to create my views using SwiftUI, since it's a lot faster than figuring out CGPaths, pixel perfect anti-aliasing, etc. Thanks!
0
0
1.2k
Sep ’21
Rendering Surface Bounds determination
Good day, simple question. In TextKit2, there seems to be a Rendering Surface Bounds property. We currently have a text.boundingRect(with...) function call to determine some bounding properties, and this is the accepted proposal for most bounding retrieval, but I am not sure the operation is actually the Rendering Surface Bounds of a provided text, as described in the video. So ... let say I have such a text, and I want to get that fully enclosing bounds, what would be my best bet (cross-platform)
0
0
864
Jul ’21
The method updateContentSizeIfNeeded not set correct size
The iOS demo project using updateContentSizeIfNeeded to get the layout frame and set the content size for text view. However, it looks like the height is not correct. There is always a huge spacer from the last paragraph to scroll view content bottom. (a.k.a calculated frame large than the real frame. I'm trying to use the TextKit 2 to layout the text without scrolling enabled. Should I use the old way NSAttirubtedString's boundingRect or there is some new method to calculate the frame size. Thanks.
1
0
897
Jul ’21
The example project of TextKit 2 did't implement the indented, nested comments like the one shown in the session
Hi there, The example project provided along with the session video doesn't behave like the one shown in the video: the comments are not indented. I couldn't find where the commentDepth value is being used in drawing anywhere. Is it possible to get an updated version with the indentation implemented of this example project? Thanks.
2
0
1.3k
Jul ’21
UITextView.layoutManager and availability of TextKit2.0 on iOS
We have and app that is using UITextView.layoutManager quite a lot. Does that means that in next version of iOS, when TextKit2.0 is available for UITextView, we will have to update the app? Will old, not updated app on AppStore still work without updates? What are the reasons it is not available on iOS15? P.S. TextKit1.0 in AppKit have a lot more functionality than UIKit version. Would be great to have feature parity. With ARM CPU in both Macs and iPads there is no reason not to.
1
0
1.2k
Jun ’21
NSLayoutManager.drawGlyphs() can not display MarkDown attributed string
We use NSLayoutManager/NSTextStorage/NSTextContainer to display markdown attributed string on a canvas. MarkDown string was processed properly, but NSLayoutManager just exhibits the plain text. The code is as follows:     var layoutmanager = NSLayoutManager()     var textstorage = NSTextStorage()     var textcontainer = NSTextContainer()   var attrStr = try AttributedString.init(markdown: "**test**", options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace)) var attrString = NSMutableAttributedString(attrStr)  self.textstorage.setAttributedString(attrString!) // draw text layout     let range = self.layoutmanager.glyphRange(for: self.textcontainer)     self.layoutmanager.drawBackground(forGlyphRange: range, at: self.textLocation)     self.layoutmanager.drawGlyphs(forGlyphRange: range, at: self.textLocation) Is it because TextKit 1 does not support markdown string display or I miss something else ? Any help will be appreciated. Thanks!
Replies
1
Boosts
0
Views
1.5k
Activity
Nov ’21
TextKit 2 documentation update?
The documentation for TextKit 2 is a bit thin. I have a few questions about that: Is more complete documentation available to Developer Program members? When can developers expect for the API documentation to be updated? When will overview documentation, like TextKit 1 has, be available?
Replies
0
Boosts
0
Views
925
Activity
Nov ’21
Adding un-selectable text to NSTextParagraph?
I want to add characters to the display of my paragraphs without modifying the underlying text storage. To be concrete, let's say that I want to prefix each paragraph with "Whereas, ". Further, when I drag out a text selection on a paragraph that's displayed this way, I do not want "Whereas, " to receive a selection highlight. Using TextKit 2, it's pretty easy to accomplish the first part using a NSTextContentStorageDelegate whose method textContentStorage(_: NSTextContentStorage, textParagraphWith: NSRange) -> NSTextParagraph? adds my prefix to each paragraph. I modified the WWDC21 demo program to do it. I appreciate how I can avoid glyph munging by using TextKit 2 instead of TextKit 1, it will save a lot of trouble. If I drag a selection across the prefix, then it receives the selection highlight. What's more, when I drag a selection across the end of the paragraph, the last eight characters (and, I presume, the invisible newline) do not receive the selection highlight. So adding the prefix has skewed the selection indices. It seems to me that the TextKit 2 designers may intend for me to use a NSTextLocation subclass consisting of a "storage" index and a "display" sub-index. On the storage indices there are the characters of the text storage, and at (storage index, display sub-index)-pairs are the characters of my prefix. While the user drags out a selection, I should subtract from the NSTextSelection any NSTextRanges belonging to non-storage text such as my prefix. Am I on the right track, so far?
Replies
0
Boosts
0
Views
924
Activity
Nov ’21
Multipage Layout of Text on iOS and MacOS with TextKit 2
Hi! First apologies if I may have missed this in the forums somewhere, but I have been unable to find any documentation on multi-page layouts with the newer text kit and have not been able to create this using/modifying the sample code provided from the WWDC conference earlier this year. What I am trying to do is create a multipage document where the text is editable and the layout is fairly static. When the text expands in the current view to where there is no room left, a new 'page' will be created and the text will spill over there. Basically standard word or google docs type editor. The position for the pages will need be manipulated depending on some properties. I also intend in wrapping the solution in SwiftUI since the rest of the app this will be used in is a SwiftUI app. If this isn't possible, I may just stick with TextKit1 or punt altogether. Thanks! Sean
Replies
2
Boosts
0
Views
1.2k
Activity
Nov ’21
TextKit2: Strange behaviour in directional text selection navigation
Hi there, I am currently trying to extend the TextKit 2 demo project from WWDC 2021 to allow keyboard input under iOS using UITextInteraction. For the most part, it is working well, but the behaviour of the directional cursor text navigation is strange: When moving up or down, it keeps the horizontal location within the current paragraph. But when crossing an empty line into another paragraph, the former horizontal position is lost and the cursor is reset to the line start. This is a video showing the behaviour: https://www.icloud.com/iclouddrive/0Xwf9hpb2DcAoU1CNPZnVgwQw#TextKit2_Selection_Bug I would expect the cursor to restore its former horizontal position in following paragraphs. This is the standard behaviour in UITextView and NSTextView. Does anybody know if this a bug in TextKit2 or if there is a way to achieve the expected beahviours. Thanks Frank
Replies
1
Boosts
0
Views
1.4k
Activity
Nov ’21
Custom paste into UITextView
I am implementing a UITextView subclass that supports the editing of structured text with block styles (e.g. paragraph, heading, list, quote etc.) and inline styles (e.g. emphasis, code etc.) I need to be able to cut, copy and paste structured text from the UITextView. Implementing this is straightforward: override cut(_:), copy(_:) and paste(_:) and read/write the structured text (a JSON format) to/from the general pasteboard. This works fine, but the UITextView's smart insert/delete behavior (see UITextInputTraits.smartInsertDeleteType), which adjusts spaces around the selection, is lost. I have explored implementing a custom text paste delegate (UITextPasteDelegate). This would appear to provide support for custom paste behaviour. I can to extract the contents of the pasteboard and transform them to an attributed string for pasting by implementing textPasteConfigurationSupporting(_:, transform:) however any custom attributes I add to the string placed in the UITextPasteItem are ignored (standard attributes are also ignored because allowsEditingTextAttributes is set to false). I can solve this issue by implementing textPasteConfigurationSupporting(_:, performPasteOf:, to:) but this breaks the smart insert/delete behaviour, even when pasting plain text. Unfortunately I can see no way to paste custom textual data into a UITextView without losing the standard smart insert/delete behaviour. Am I missing something? Any pointers on how I can achieve cut/copy/paste for a custom clipboard format while maintaining smart insert/delete would be appreciated.
Replies
0
Boosts
0
Views
1.6k
Activity
Oct ’21
What is difference between lineFragmentRect and lineFragmentUsedRect in TextKit?
Recently, I've started to learn deep TextKit APIs and concepts, such as typeface, typesetter, Text Layout and so on. I found APIs named lineFragmentRect and lineFragmentUsedRect, however I can't understand these conceptual difference. (These values are bit of different.) Cloud you tell me them if you know something about it?
Replies
2
Boosts
0
Views
1.4k
Activity
Oct ’21
SF Pro in Figma
Hello, I have previous versions on SF fonts installed on my Mac, including SF Pro Display. I recently downloaded the updated SF Pro, but it does not show up in Figma. I can see it and use it fine in Sketch. Does anyone know how to resolve this issue so I can access it in Figma?
Replies
3
Boosts
0
Views
11k
Activity
Sep ’21
TextKit 2 and SwiftUI
Is it possible to use/draw a SwiftUI view in TextKit 2? The NSTextLayoutManagerDelegate seems to be built around returning a NSTextLayoutFragment. Since NSTextLayoutFragment is a class, I can't subclass it and use SwiftUI. I'd love to create my views using SwiftUI, since it's a lot faster than figuring out CGPaths, pixel perfect anti-aliasing, etc. Thanks!
Replies
0
Boosts
0
Views
1.2k
Activity
Sep ’21
Capturing a word that is being "looked up"?
iPhones have this innate feature where you can long-press on a word and select look-up to see its definition. Is there a way to store what word is currently being looked-up/which words have been looked-up in the past, using some kind of query/framework?
Replies
0
Boosts
0
Views
681
Activity
Aug ’21
Rendering Surface Bounds determination
Good day, simple question. In TextKit2, there seems to be a Rendering Surface Bounds property. We currently have a text.boundingRect(with...) function call to determine some bounding properties, and this is the accepted proposal for most bounding retrieval, but I am not sure the operation is actually the Rendering Surface Bounds of a provided text, as described in the video. So ... let say I have such a text, and I want to get that fully enclosing bounds, what would be my best bet (cross-platform)
Replies
0
Boosts
0
Views
864
Activity
Jul ’21
The method updateContentSizeIfNeeded not set correct size
The iOS demo project using updateContentSizeIfNeeded to get the layout frame and set the content size for text view. However, it looks like the height is not correct. There is always a huge spacer from the last paragraph to scroll view content bottom. (a.k.a calculated frame large than the real frame. I'm trying to use the TextKit 2 to layout the text without scrolling enabled. Should I use the old way NSAttirubtedString's boundingRect or there is some new method to calculate the frame size. Thanks.
Replies
1
Boosts
0
Views
897
Activity
Jul ’21
The example project of TextKit 2 did't implement the indented, nested comments like the one shown in the session
Hi there, The example project provided along with the session video doesn't behave like the one shown in the video: the comments are not indented. I couldn't find where the commentDepth value is being used in drawing anywhere. Is it possible to get an updated version with the indentation implemented of this example project? Thanks.
Replies
2
Boosts
0
Views
1.3k
Activity
Jul ’21
How do I tell a NSTextView to update its textStorage while It is waiting for input?
When a user is editing a NSTextView and commits some kinds of operations (e.g. Quiting an app), the NSTextView's textStorage does not contain the current contents of the NSTextView. How do I get the current contents?
Replies
1
Boosts
0
Views
1.7k
Activity
Jun ’21
Image in Text View
Hi! I have question. How can I insert image to text view with smaller size(scaled to text view), but with keeping quality? Like in Notes on iOS - picture with high quality weight around 800kB. Thanks.
Replies
1
Boosts
0
Views
3.1k
Activity
Jun ’21
UITextView.layoutManager and availability of TextKit2.0 on iOS
We have and app that is using UITextView.layoutManager quite a lot. Does that means that in next version of iOS, when TextKit2.0 is available for UITextView, we will have to update the app? Will old, not updated app on AppStore still work without updates? What are the reasons it is not available on iOS15? P.S. TextKit1.0 in AppKit have a lot more functionality than UIKit version. Would be great to have feature parity. With ARM CPU in both Macs and iPads there is no reason not to.
Replies
1
Boosts
0
Views
1.2k
Activity
Jun ’21