tags:

views:

121

answers:

1

Shark shows me that this method takes a HUGE performance hit. Like 80% in my table view. All I do is draw two labels per cell (about 8 in total per page). During scrolling. I drawInRect: with that.

Are there some better methods? Like drawing directly to some layer?

+1  A: 

You can do faster drawing using code like the following:

CGContextSetStrokeColorWithColor(context, strokeColor); 
CGContextSetFillColorWithColor(context, strokeColor);

CGContextSelectFont(context, "Helvetica", fontSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextPosition(context, 0.0f, round(fontSize / 4.0f));
CGContextShowText(context, [text cStringUsingEncoding:NSMacOSRomanStringEncoding], strlen([text cStringUsingEncoding:NSMacOSRomanStringEncoding]));     

However, the Mac Roman encoding for the Helvetica font on the iPhone is missing symbols for many non-English characters, so I think you're stuck with the NSString drawing methods unless you want to step up to Core Text in iPhone OS 3.2.

To check and see if your string can be represented using the Mac Roman character set, use something like the following:

if ([text canBeConvertedToEncoding:NSMacOSRomanStringEncoding])
Brad Larson
is helvetica the font used by -systemFontOfSize: ?
dontWatchMyProfile
why round(fontSize / 4.0f) ?
dontWatchMyProfile
@mystify - Yes, I believe Helvetica is the default system font on the iPhone. As far as the spacing, that's what I found I needed to match the baseline position of text drawn this way with the NSString drawing methods.
Brad Larson