Post not yet marked as solved
Thanks for the response.
I have done as suggested, and the Feedback ID is FB10681825
Post not yet marked as solved
I wasted too much time on this problem, because I missed one very important step. In case it saves someone else some pain, when you provide your widget with dynamic options, you must do that in a separate Intent target in your app. I had just added the IntentHandler straight to the widget extension target, and couldn't figure out why it wasn't working.
Dumb mistake, but also one that leads to the above behavior.
Spoke too soon, as it turns out. Using layoutManager:shouldBreakLineByWordBeforeCharacterAtIndex: is effective if the trailing text is longer than the remaining space on the line. In this case, it does what I described, and you can choose where to put the line break.
The problem is, if the trailing text is short, and fits on the line after the attachment, the layout manager doesn't even look for a line break. It isn't needed, and so there is no way to intervene.
Think inserting line break characters is the most foolproof approach.
I apologize. You are quite correct that you can use layoutManager:shouldBreakLineByWordBeforeCharacterAtIndex: to get the trailing text to wrap. I just needed to "wrap" my head around what that method was doing.
For anyone wondering, the trick is to keep returning false from the delegate method until it passes in the word just after your attachment, at which point you return true. The layout manager will start with the last word on the line, and work back. By returning false, you are telling it you want to break earlier in the line. When you reach the point you actually want to break, you return true to indicate to make the wrap.
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. This indeed works. It was one of the approaches I had tried. The problem is then the trailing text...
In order to push out characters following the attachment, you can use layoutManager:shouldBreakLineByWordBeforeCharacterAtIndex:. I tried this, but it seems unrelated to the problem. This method seems to be about wrapping policy; it doesn't give you a way to actually force a wrap at any given character or word boundary, which is more what I need.
I also experimented with layoutManager(_:, shouldSetLineFragmentRect:, lineFragmentUsedRect:, baselineOffset:, in:, forGlyphRange:), but couldn't get that working either.
I wish there was just a method very similar to the attachment bounding rect one for standard text layout. Then it would be trivial. But I can't find a way to intervene in the text layout to cause it to wrap. I think inserting line breaks (which is what I did up until now) is probably the only way.
Thanks. That is one of the things I already tried. You are right that this achieves the goal of having the attachment alone on the line.
The problem with that approach is that you then can't put the cursor behind the attachment. The cursor only goes right at the end of the line. I note that apps like Apple Notes can put the cursor behind the attachment (not at the end of the line), so I assume it is possible.