Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering CoreText within an irregular shape

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://stackoverflow.com/q/3048305

  • CoreText's CTFramesetter does not support rendering into a CGPath

    https://stackoverflow.com/q/3813318

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.

like image 547
Justin Avatar asked Mar 01 '11 17:03

Justin


1 Answers

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).

like image 65
Amy Worrall Avatar answered Nov 02 '22 12:11

Amy Worrall