I have a class that works to parse text into multiple pages. I use the sizeWithFont: method to identify when one page ends and another starts. But, unfortunately, since the amount of text that needs to be parsed is quite large, the whole operation must be performed in the background thread (it takes several seconds to complete). And so sometimes I get visual artifacts on my interface (UIKit is not thread safe, but I call it from several threads at the same time), which I would like to get rid of.
I need to get rid of using sizeWithFont: in the background thread. But there is simply no alternative for this method. The only way to find out the width of text using Core Graphics is to use the method specified in the Apple documentation:
- Call the CGContextGetTextPosition function to get the current position of the text.
- Set the text drawing mode to kCGTextInvisible using the CGContextSetTextDrawingMode function.
- Draw text by calling the CGContextShowText function to draw text at the current text position.
- Determine the end position of the text by calling the CGContextGetTextPosition function.
- Subtract the starting position from the ending position to determine the width of the text.
But I am really worried that this will lead to a huge loss in performance.
Does anyone know another way to find out the width of the text?