How to measure CG text

Q: How do I measure the width of my text before drawing it with Core Graphics?

A: First call CGContextGetTextPosition to find the current text position. Then, set the text drawing mode to kCGTextInvisible using CGContextSetTextDrawingMode and draw the text. Finally, call CGContextGetTextPosition again to determine the final text position. Take the difference between the starting and ending positions to find the width of your text.

Document Revision History

2007-08-02minor cleanup and made 'Not Recomended'
2001-07-02Explains how to measure Core Graphics text.

Posted: 2007-08-02

Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.