1

I have a Mac app with an embedded rich-text NSTextView, but the same problem can be reproduced with TextEdit:

  • When a line contains only an image and no text, typing right after the image causes the text below to move downwards.

TextEdit

  1. Open a new TextEdit document and type "test", [return]
  2. Cmd-Shift-Ctrl-4 to capture a screenshot of something
  3. Paste the image on the next line
  4. [return], type "test"
  5. Now type something after the image

To debug this in my app I added some code to highlight each line fragment rect via NSLayoutManager:

Custom app with NSTextView

The height of line 2 starts out at 14pt, as soon as I type a letter, it changes to 18pt.

Is this a bug in TextKit or a problem with incorrect/missing NSAttributedString attributes in my code?

I tried setting a paragraph style and font for my inline image, as well as a default paragraph style for the NSTextView, but the problem still occurs.

Mark
  • 6,647
  • 1
  • 45
  • 88
  • I suspect it's because text has *leading* and an image doesn't. The leading for the line is going to be the largest leading of each character/glyph on that line. – James Bucanek Nov 27 '18 at 18:00
  • @James thanks for your suggestion. Would you know of a way how I can override the leading for attachments? An image is represented by a single character (`NSTextAttachment.character`). If I add that character without an image attachment to my document, the line height is correct. So I suspect it is a problem with calculating the height of an NSTextAttachment. – Mark Nov 29 '18 at 14:57
  • First of all, this isn't a bug. There's no "problem with calculating the height" of an `NSTextAttachment`. The x-height and leading of a line are the maximum x-height and leading of the text on that line; and attached images aren't text so they have an x-height but no leading. This is the way text editing has always worked, going back to NeXT Step. – James Bucanek Nov 29 '18 at 16:38
  • Having said that, you might try playing some games with the placement of the image so it appears to have leading. I'd experiment with adding a subscript attribute to the attachment to make is draw below the baseline, then fiddle with size of the attachment to make it taller than the image, then fiddle with the bounds so the image draws higher (i.e. so it's bottom is on the baseline again). You might even create your own attachment drawing cell, but this is all super-hacky and you're swimming upstream. – James Bucanek Nov 29 '18 at 16:42

0 Answers0