I have a problem similar to this question but more complex. I'm trying to draw text in a table view cell which has a date label top right and a badge bottom right, like so:
+-----------------------+----+
| Lorem ipsum dolor sit |DATE|
| amet, consectetur +----+
| adipiscing elit. Integer |
| ligula lectus, convallis |
| non scelerisque +-----+
| quis, tempor at nibh |BADGE|
+----------------------+-----+
I want the cell to be larger or smaller if there is more or less text:
+-----------------------+----+
| Lorem ipsum dolor sit |DATE|
| amet, consectetur +----+
| adipiscing elit. Integer |
| ligula lectus, convallis |
| non scelerisque quis, |
| tempor at nibh. Curabitur |
| eget diam ligula. |
| Pellentesque a elit |
| dolor, a ullamcorper +-----+
| risus. |BADGE|
+----------------------+-----+
The answer to the older question links to a project which shows how to flow text around known shapes. But I don't know the shape in advance, as I need the badge to align with the bottom of the text.
Is this even possible? If so, how do I draw the text, and how do I work out how tall it will be?
Your case is not too bad since everything is a rectangle and it appears that the width is fixed. For some starter code, see ColumnView.
Here's how I would attack it:
CTFrameSetter
. Create a path for the top box and create a CTFrame
to lay that out. That part is certain, so hold onto it. If all the text fit, then you're done.CTFrameSetter
to create frames for the middle and bottom frames. Ask it how much text it was able to layout. If it finished everything, you're done. This is the smallest box you're willing to draw.There are a few places to optimize in there, but it's probably faster than you think.