Before I ask my questions, this is from Apple\'s documentation re: how to determine the width of a string using Quartz:
If text measurements are important
When performing drawing operations on threads other than the UI thread, you must use the technique you described. This is especially important to note when using things like CATiledLayer, which performs it's rendering asynchronously on background threads, or when drawing custom graphics in the background for any other reason.
I agree with PGB when you're doing "simple" graphics and you are guaranteed to be running on the main thread. However, sizeWithFont is doing the same thing as the technique you described - it's simply doing it for you using UIGraphicsGetCurrentContext().
On the iPhone SDK, there's a family of methods on NSString
that provide what you want.
As of iOS 7.0, these methods are:
- boundingRectWithSize:options:attributes:context:
- sizeWithAttributes:
On older versions of iOS we had these, now deprecated:
– sizeWithFont:
– sizeWithFont:forWidth:lineBreakMode:
– sizeWithFont:constrainedToSize:
– sizeWithFont:constrainedToSize:lineBreakMode:
– sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: