views:

165

answers:

1

Hi!

I have a class which work is to parse a text into several pages. I use sizeWithFont: method to identify when one page ends and another starts. But unfortunately since the amount of text which needs to be parsed is pretty big, whole operation needs to be performed on a background thread (takes several second to complete). And therefore sometimes I get visual artifacts on my interface (UIKit is not thread safe but I call it from several threads simultaneously), which I would love to get rid of.

I need to get rid of using sizeWithFont: on background thread. But there just doesn't seem to be an alternative for this method. The only way to find out the width of the text with Core Graphics is to use method stated in apple's documentation:

  1. Call the function CGContextGetTextPosition to obtain the current text position.
  2. Set the text drawing mode to kCGTextInvisible using the function CGContextSetTextDrawingMode.
  3. Draw the text by calling the function CGContextShowText to draw the text at the current text position.
  4. Determine the final text position by calling the function CGContextGetTextPosition.
  5. Subtract the starting position from the ending position to determine the width of the text.

But I'm really concerned that this will lead to a huge performance loss.

Anyone knows another way to find out text width?

+1  A: 

If you have the possibility to target your application for iPhone OS 3.2, you can take a look at Core Text.

Although, note that the entire Core Text library isn't completely thread safe either. From the documentation:

Multicore Considerations: All individual functions in Core Text are thread safe. Font objects (CTFont, CTFontDescriptor, and associated objects) can be used by simultaneously by multiple operations, work queues, or threads. However, the layout objects (CTTypesetter, CTFramesetter, CTRun, CTLine, CTFrame, and associated objects) should be used in a single operation, work queue, or thread.

alleus