Search code examples
iphonemultithreadingcgcontextcore-text

Core Text: counting pages in background thread


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?


Solution

  • You don't need to CTFrameDraw before getting result from CTFrameGetVisibleStringRange