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:
- Call the function CGContextGetTextPosition to obtain the current text position.
- Set the text drawing mode to kCGTextInvisible using the function CGContextSetTextDrawingMode.
- Draw the text by calling the function CGContextShowText to draw the text at the current text position.
- Determine the final text position by calling the function CGContextGetTextPosition.
- 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?