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

Posts under TextKit tag

169 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
948
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
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
949
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
948
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.5k
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