Search code examples
ioscore-text

Core Text: Drop caps + paragraph styles: Incompatible?


I'm attempting to draw a richly laid out text view on iPhone that features:

  1. Custom paragraph spacing (kCTParagraphStyleSpecifierParagraphSpacing)
  2. Custom paragraph first-line indentation (kCTParagraphStyleSpecifierFirstLineHeadIndent)
  3. Justified alignment (kCTParagraphStyleSpecifierAlignment)
  4. Finally, a drop cap on my first paragraph

I'm using OHAttributedLabel. The first three points I achieved without much trouble by just setting some paragraph style attributes on my NSAttributedString.

The drop cap I managed to implement by hacking OHAttributedLabel:

  1. Cut out a rectangular region out of the main paragraph's CGMutablePathRef the size of the drop cap by adding an extra CGPathAddRect, as detailed in this excellent blog post.
  2. Drawing the large character in this region with an extra CTFrameDraw call.

My problem: The paragraph styles and the custom text path are incompatible. When I cut a rectangular chunk out of the main text's path, all the paragraph styles seem to get thrown away.

Does anyone know a way to make them work together? Or can anyone think of another way to implement drop caps? (Short of using a UIWebView + CSS, which I'd rather not have the overhead of!)

Thanks!


Solution

  • You can use straight Core Text to achieve this, in the following post I explain the use of 2 framesetters to lay out text with drop caps in a UIView. In the code example (there's also a link to a github repo) you'll be able to see where the paragraph styles are created and applied to the main text view.

    https://stackoverflow.com/a/14639864/1218605