Can I edit NSTextLineFragment position or update manually in TextKit2

In my app, User can select word in the UITextView, then I want to insert a content under the selected words(the comment words shouldn't be selected).like:

I found TextKit2 only support edit NSTextParagraph position, or I missed some features in NSTextLayoutManager?

I try to override the NSTextLayoutFragment and update the draw(at point: CGPoint, in context: CGContext) but I found, user still can select the origin content at the origin position, even if the layer of linefragment layer on the corrent position not the origin position.

Answered by DTS Engineer in 823301022

Are you trying to implement a custom NSTextLineFragment class to return a different typographicBounds or glyphOrigin for your comment text line? I haven't really tried that, but since you already have a custom NSTextLayoutFragment class, you can try to override textLineFragment(for:isUpstreamAffinity:) and textLineFragment(forVerticalOffset:requiresExactMatch:) to provide your own text line fragments, which I believe should do the trick.

Alternatively, you can probably make the text after the line that contains the word being commented a new paragraph, and make the comment a paragraph in between. With that, you convert the original paragraph into three, and can render them with three NSTextLayoutFragment. The Using TextKit 2 to interact with text sample demonstrates how to do that.

I'd imagine that if your text fragments have the right bounds, the text selection will do the right work – If not, I'd be interested in looking into your code, assuming you can provide a minimal project that demonstrates the issue.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

Are you trying to implement a custom NSTextLineFragment class to return a different typographicBounds or glyphOrigin for your comment text line? I haven't really tried that, but since you already have a custom NSTextLayoutFragment class, you can try to override textLineFragment(for:isUpstreamAffinity:) and textLineFragment(forVerticalOffset:requiresExactMatch:) to provide your own text line fragments, which I believe should do the trick.

Alternatively, you can probably make the text after the line that contains the word being commented a new paragraph, and make the comment a paragraph in between. With that, you convert the original paragraph into three, and can render them with three NSTextLayoutFragment. The Using TextKit 2 to interact with text sample demonstrates how to do that.

I'd imagine that if your text fragments have the right bounds, the text selection will do the right work – If not, I'd be interested in looking into your code, assuming you can provide a minimal project that demonstrates the issue.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

You are right insert a new paragraph is a better way to insert comment. But when I add these to UITextView, and I want to support select action to UITextView, the comment will be selected.

is there any good way to ignore insert paragraph when user are selecting words?

Can I edit NSTextLineFragment position or update manually in TextKit2
 
 
Q