I'm looking for guidance on implementing a view that renders an NSAttributedString
within a polygon with holes, wrapping and reflowing text to fit the geometry. It's not CoreText
that's the issue, but the general problem of partitioning an irregular shape into an ordered sequence of squat rectangles.
Similar questions haven't been answered fully:
How to fill a shape with text in Javascript
https://mcmap.net/q/553430/-algorithm-for-text-wrapping-within-a-shape
CoreText's
CTFramesetter
does not support rendering into aCGPath
https://mcmap.net/q/672992/-core-text-render-to-an-odd-shape
CoreText handles an unbelievable amount of the grunt work associated with text layout and display, so I can't help but suspect that I'm reinventing a wheel. For the purposes of this question, please assume that I can check the substring that fits within a given rectangle, taking into account word wrap and hyphenation.
Edit: I've since decided to just sweep left-to-right drawing as much as fits between boundaries. It looks a bit haphazard even though I'm breaking at natural word boundaries, so I'd still appreciate guidance on how other applications wrap text.
Edit #2: It looks decent now that it supports basic word wrap and avoids rendering very short lines. My question must have been too vague. Thanks for looking.
Edit #3: Amorya points out that CTFramesetter
now accepts any CGPath
.
CGPath
. That's a proper solution. Thanks for revisiting the question after so long. – Menorah