views:

66

answers:

1

Hi,

I'm having a difficult time trying to work out how to build a page using Core Text, where I have multiple paragraphs which follow one another, but in different styles.

In other words, I would like to have a title paragraph, followed by a subtitle paragraph, followed by several body paragraphs. In HTML terms, this would be:

<h1>Some title</h1>
<h2>Some subtitle</h2>
<p>Blah blah...
         ...</p>

I have got as far as creating a CTFramesetter for the title, creating a CTFrame from that, and then drawing it to the context. However I don't understand how to create a new frame which flows on from the previous paragraph. Can anyone help please? Or is there some good online tutorial to help?

Thanks!

:-Joe

+1  A: 

The easiest way to do this is to just style your NSAttributedString with the different styles before you create any framesetters.

cobbal
Do I create a single NSAttributedString for the whole document?
Joe
@Joe If you can, that's the best way to do it.
cobbal
Thanks :) are there any good examples of how to build up an NSAttributedString from HTML or similar?
Joe
@Joe Not that I can find. The basic idea is to have a NSMutableAttributedString and append strings and styles to it as you build it.
cobbal
I've managed to get so far, but I can't get it to add paragraph styles (I need some text justified and others not)... Doesn't appear as though I can use NSParagraphStyle in iOS :(
Joe
@Joe you should be able to use CTParagraphStyle http://developer.apple.com/library/ios/#documentation/Carbon/Reference/CTParagraphStyleRef/Reference/reference.html The list of attributes you can use is at http://developer.apple.com/library/ios/#documentation/Carbon/Reference/CoreText_StringAttributes_Ref/Reference/reference.html
cobbal
I sorted it... Converted to CFAttributedString, added the paragraph styling, then converted it back again! :)
Joe