I want to setup a UITextView so that image attachments get a line completely to themselves, without any text on the same line. This is trivial if the image is just as wide as the line, by simply ensuring the bounding box of the attachment returns the full width, but I haven't found a good way to do it when the image is less than full width.
Here is what I wish to achieve visually. https://www.dropbox.com/s/wktuxlod1rnyps1/Screen%20Shot%202020-06-29%20at%2015.14.26.png?dl=0
One way I have found that works OK, but feels like a hack to me, is inserting line breaks. I would much prefer to implement NSLayoutManager or NSTextAttachment methods to achieve this.
Another way to make it work is to return the full width from the NSTextAttachment bounding box method, but then just display the image in part of that area. The downside with this approach is that if you put the cursor at the end of the line, it doesn't sit next to the drawn image, but there is a gap and the cursor appears completely to the right.
Finally, I tried returning two different widths from the NSTextAttachment as the bounds: the first was the full line fragment width, to force a wrap, and on the next call I return the actual width of the image. This seems to give me most of what I want. The downside is that the next line of text will creep up to appear behind the image on the same line. I really need a way to force that text to the next line.
What is the sanctioned way to achieve this?
Here is what I wish to achieve visually. https://www.dropbox.com/s/wktuxlod1rnyps1/Screen%20Shot%202020-06-29%20at%2015.14.26.png?dl=0
One way I have found that works OK, but feels like a hack to me, is inserting line breaks. I would much prefer to implement NSLayoutManager or NSTextAttachment methods to achieve this.
Another way to make it work is to return the full width from the NSTextAttachment bounding box method, but then just display the image in part of that area. The downside with this approach is that if you put the cursor at the end of the line, it doesn't sit next to the drawn image, but there is a gap and the cursor appears completely to the right.
Finally, I tried returning two different widths from the NSTextAttachment as the bounds: the first was the full line fragment width, to force a wrap, and on the next call I return the actual width of the image. This seems to give me most of what I want. The downside is that the next line of text will creep up to appear behind the image on the same line. I really need a way to force that text to the next line.
What is the sanctioned way to achieve this?
You can utilize the contextual information passed into the method. The method can return the wide (text container) width when asked for a position training other characters in the line to push the attachment out to the next line. It can return its natural size in other cases. In order to push out characters following the attachment, you can use -layoutManager:shouldBreakLineByWordBeforeCharacterAtIndex:.