Rendering CoreText within an irregular shape
Asked Answered
M

1

5

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:

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.

Menorah answered 1/3, 2011 at 17:24 Comment(0)
S
6

I wrote a blog post about achieving text wrap with Core Text:

http://blog.amyworrall.com/post/11098565269/text-wrap-with-core-text

The feature is new in iOS 4.3 and MacOS X Lion. You can now firstly draw inside non-rectangular paths, and secondly pass in other paths to mask the flow (i.e. be the holes you wrap around).

Shoreline answered 11/10, 2011 at 8:49 Comment(2)
Glad to see it now accepts an arbitrary CGPath. That's a proper solution. Thanks for revisiting the question after so long.Menorah
No worries. I was searching for something myself, and couldn't find any questions about it apart from old ones. I don't think they publicised the change very well.Shoreline

© 2022 - 2024 — McMap. All rights reserved.