views:

27

answers:

1

I need to be able to read text that has been tagged using XML for formatting and display it on the iPad formatted. The one trick is that I need to support footnotes.

I need to be able to read source, parse source, create attributed string for source, create lines. Then I need to write lines to either the text or footnotes until the screen is filled.

I am told that core text is what I need to use. However, like the rest of Apple's documentation, the Core Text documentation is simply embarrassing.

There is a framesetter class that formats text to fit in a frame (appears not to be what I want to do). There is a typesetter class that formats lines. But there appears to be no way to specify the width of the line.

Considering the variety of applications that would need to do exactly this, I cannot find any examples whatsoever. Some of the core text functions that are only "documented" in the vaguest terms generate no Google hits except for the apple "documentation."

Can someone explain how one would accomplish this programming tast?

Thanks

A: 

There are a bunch of potential approaches, but one would be to use CTTypeSetter. The detail you're missing is the CTTypeSetterSuggestLineBreak and CTTypeSetterSuggestLineBreakWithOffset functions. Those do take a width argument.

Another approach is to use CTFrameSetter, which just uses a CTTypeSetter internally anyway. You can then call CTFrameGetLines on the frame you get from the frame setter and render the lines individually with CTLineDraw. Use CGContextSetTextPosition to tell Core Graphics where to draw the each line. If you want to figure out which characters in your original string are represented by each line, you can use CTLineGetStringRange.

Since it sounds like you're new to Core Text, note that its coordinate system has the origin at the bottom left corner of the screen and the Y axis increases as you go up the screen. The rest of UIKit uses the opposite convention (origin at top left, Y axis increases as you go down). You'll want to use CGContextScaleCTM( context, 1, -1 ) and CGContextTranslateCTM to correct for that. You'll also probably want to call CGContextSetTextMatrix( context, CGAffineTransformIdentity ) to make text render as expected.

Jacques