[iPhone] Custom -sizeWithFont: and CGContextRef question
[iPhone] Custom -sizeWithFont: and CGContextRef question
- Subject: [iPhone] Custom -sizeWithFont: and CGContextRef question
- From: Michael Hoy <email@hidden>
- Date: Thu, 16 Jul 2009 14:30:23 -0400
Hello all,
For various reasons I'd like to implement a method similar in function
to NSString's -sizeWithFont: (UIKit addition). The difference is it
takes a CGFontRef. Here's a rough idea of the code (a slight
adaptation of Timothy Ritchey's TTFontCell code, and I apologize
because I can't find a link to it right now):
- (CGSize)sizeWithFont:(CGFontRef)font ofSize:(CGFloat)size
withKerning:(CGFloat)kerning {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFont(context, font);
CGContextSetFontSize(context, fontSize);
CGContextSetCharacterSpacing(context, kerning);
....
}
The code works — however, it can only be called within a UIView's
drawRect: method, for a valid context to be returned in
UIGraphicsGetCurrentContext();
This method is only useful really if it can be called *before* the
drawing code, for laying out views and such. I know that I need to
create a CGContextRef and push it on the UIGraphics stack. My question
is, what might be the best way to do this? I see I can use
CGPDFContextCreate or CGBitmapContextCreate... which makes sense here?
All I need to do is determine the size of a string w/ specified font,
not actually draw it.
Thanks,
Michael Hoy
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden