views:

79

answers:

1

Let's say I'm writing text viewer for the iPhone using Core Text. Every time user changes base font size I need to count how many pages (fixed size CGRects) are needed to display the whole NSAttributedString with given font sizes.

And I would like to do this in separate NSOperation, so that user does not experience unnecessary UI lags.

Unfortunately, to count pages I need to draw my frames (CTFrameDraw) using invisible text drawing mode and then use CTFrameGetVisibleStringRange to count characters. But to draw a text I need a CGContext. And here the problems begin...

I can obtain a CGContext in my drawRect by calling UIGraphicsGetCurrentContext, but in this case:

  1. I have to call any method that operates on the CGContext using performSelectorOnMainThread, right?
  2. The other thread should CFRetain this context. Is it acceptable to use drawRect's CGContext outside drawRect method?

Any other solutions? Creating separate CGContext in the worker thread? How? CGBitmapContext? How can I be sure that all conditions (i don't know, resolution? etc.) will be the same as in drawRect's CGContext, so that pages will be counted correctly?

A: 

You can use CTFramesetterSuggestFrameSizeWithConstraints.

See my question here: http://stackoverflow.com/questions/3812692/how-to-split-long-nsstring-into-pages

Olof Hedman
Don't trust CTFramesetterSuggestFrameSizeWithConstraints! See here: http://web.archiveorange.com/archive/v/nagQXwVJ6Gzix0veMh09
Łukasz Sromek
As far as I understand, its just the CGSize that is bad. In my code I ignore the returned size, I use the original size I used as constraint, and it seems to work fine so far. Havn't tested it extensively yet though.
Olof Hedman
I figure I don't need the returned size, since there is other ways to measure the text when you have the range that will fit in the constraint.
Olof Hedman