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

TextKit Documentation

Posts under TextKit tag

49 Posts
Sort by:
Post not yet marked as solved
3 Replies
5.3k Views
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?
Posted
by
Post not yet marked as solved
64 Replies
8.7k Views
Hi, I'm an avid user of TextEdit to keep track of my thoughts and To-Do's through my work day. The result is, I end up with a fairly long text file, but not big on size in terms of storage. After updating to Big Sur 11.0.1, I'm noticing an odd behaviour. When every I press [Delete] more than thrice, the scroll bar jumps to the top of the text, i.e. the first line in the text file. This is very frustrating, especially, when you have lot of text and it scrolls all the way to the top. The only solution I have found is to press [Delete] slowly (may be one character per second) to avoid the scroll bar jumping to the first line of the text file. Also noticed, it does not happen in a full sentence, but only when when there's a short word and a line break [Enter] above and below the word. I'm continuing with the same file I had saved on Catalina and never had this issue. Looks like a bug to me. [PS: I am a user experience designer, not a developer and only have minimal knowledge of coding. ] Thanks, Anish
Posted
by
Post not yet marked as solved
2 Replies
810 Views
Has anyone used the new SwiftUI 2.0 TextEditor with a sizeable string? I'm currently trying it with a 400K text string and typing into the TextEditor horribly slow. Each letter I type takes seconds to appear. When the text size is small (say just a few thousand characters) it's fast - so it's clearly something to do with performance. Is this a known issue? Should I be doing something to improve it? PS - I do need to use it for my app (for e.g., a Markdown editor that loads a large file)  if let t = try? String(contentsOfFile: filePathName, encoding: .utf8) {             editorText = t             Config.logger.debug("EditorView.onAppear: Read text count = \(t.count) characters")             }
Posted
by
Post not yet marked as solved
3 Replies
1.4k Views
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?
Posted
by
sjs
Post not yet marked as solved
1 Replies
426 Views
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.
Posted
by
Post not yet marked as solved
0 Replies
419 Views
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)
Post not yet marked as solved
0 Replies
370 Views
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?
Posted
by
Post not yet marked as solved
1 Replies
565 Views
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
Posted
by
Post marked as solved
1 Replies
536 Views
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!
Posted
by
Post not yet marked as solved
0 Replies
478 Views
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!
Posted
by
Post not yet marked as solved
0 Replies
698 Views
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?
Posted
by
Post not yet marked as solved
1 Replies
476 Views
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];      attachment.image = image;      attachment.bounds = CGRectMake(0, (font.capHeight - image.size.height) / 2, 15, image.size.height);      NSMutableAttributedString *imageString = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
Posted
by
Post marked as solved
2 Replies
334 Views
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?
Posted
by
Post not yet marked as solved
0 Replies
344 Views
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.
Posted
by
Post not yet marked as solved
5 Replies
1.3k Views
We are facing the below crash quite for some days, With key NSInternalInconsistencyException, after our users got updated their OS to iOS 15, for earlier versions it works fine without any crash. We are clueless as nothing in the stack call our native code and each line in the stack relates to UI. this issue is happening for most of our users repeatedly, we tried various scenarios but unable to find the root cause. Similar issues have found in the forum when upgrading from iOS 12 to 13 but they related to threading issues, this is of a kind range exception, please do the needful. Appreciate your help in advance. Fatal Exception: NSInternalInconsistencyException out of bounds characterRange {0, 0} specified to _drawTexCorrectionMarker:characterRange:atOrigin:graphicsContext: Fatal Exception: NSInternalInconsistencyException 0 CoreFoundation 0x9905c __exceptionPreprocess 1 libobjc.A.dylib 0x15f54 objc_exception_throw 2 Foundation 0x13099c _userInfoForFileAndLine 3 UIFoundation 0xa933c -[NSTextLineFragment _drawTexCorrectionMarker:characterRange:atOrigin:graphicsContext:] 4 UIFoundation 0x8b08 -[NSTextLineFragment drawTextCorrectionMarkersAtPoint:graphicsContext:] 5 UIFoundation 0x65c44 -[NSTextLineFragment drawAtPoint:graphicsContext:] 6 UIFoundation 0x69ef4 -[NSTextLineFragment drawAtPoint:inContext:] 7 UIFoundation 0x3eb80 -[NSTextLayoutFragment drawAtPoint:inContext:] 8 UIKitCore 0x649ce4 _UITextCanvasDrawWithFadedEdgesInContext 9 UIKitCore 0x77cb4c -[_UITextLayoutFragmentView drawRect:] 10 UIKitCore 0x188930 -[UIView(CALayerDelegate) drawLayer:inContext:] 11 QuartzCore 0x4d2c8 CABackingStoreUpdate_ 12 QuartzCore 0x8fe60 invocation function for block in CA::Layer::display_() 13 QuartzCore 0x91ae0 -[CALayer _display] 14 QuartzCore 0x31bc4 CA::Layer::layout_and_display_if_needed(CA::Transaction*) 15 QuartzCore 0x460b0 CA::Context::commit_transaction(CA::Transaction*, double, double*) 16 QuartzCore 0x4f174 CA::Transaction::commit() 17 QuartzCore 0x3121c CA::Transaction::flush_as_runloop_observer(bool) 18 UIKitCore 0x544c28 _UIApplicationFlushCATransaction 19 UIKitCore 0x7dead8 _UIUpdateSequenceRun 20 UIKitCore 0xe56294 schedulerStepScheduledMainSection 21 UIKitCore 0xe55760 runloopSourceCallback 22 CoreFoundation 0xbb030 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ 23 CoreFoundation 0xcbcf0 __CFRunLoopDoSource0 24 CoreFoundation 0x5ff8 __CFRunLoopDoSources0 25 CoreFoundation 0xb804 __CFRunLoopRun 26 CoreFoundation 0x1f3c8 CFRunLoopRunSpecific 27 GraphicsServices 0x138c GSEventRunModal 28 UIKitCore 0x51b060 -[UIApplication _run] 29 UIKitCore 0x298b8c UIApplicationMain 30 mobilex 0x41673c (Missing) 31 User Mobile 0x25f18 main + 43 (main.m:43) 32 ??? 0x1056f9a24 (Missing)
Posted
by
Post not yet marked as solved
2 Replies
428 Views
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
Posted
by
Post not yet marked as solved
1 Replies
426 Views
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
Posted
by
Post not yet marked as solved
0 Replies
274 Views
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?
Posted
by