Re: [iPhone] Custom -sizeWithFont: and CGContextRef question
Re: [iPhone] Custom -sizeWithFont: and CGContextRef question
- Subject: Re: [iPhone] Custom -sizeWithFont: and CGContextRef question
- From: Scott Thompson <email@hidden>
- Date: Fri, 17 Jul 2009 01:05:31 -0500
On Jul 16, 2009, at 1:30 PM, Michael Hoy wrote:
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.
Working directly from a CGFontRef is often not the Right Thing(tm) to
do primarily because Quartz 2D is not the best way to draw text.
Quartz 2D is really good at drawing glyphs but there's a long road
from characters to glyphs. On the text system, there are public APIs
to Core Text that which handle things like line layout, glyph
substitution, bidi text adjustments etc... On the iPhone you're
largely limited to what you can find in UIKit.
Can you elaborate on why you are working from a CGFontRef instead of
from a UIFont?
If you absolutely MUST work from the CGFont level, you are likely to
have to use routines like CGFontGetGlyphBBoxes and
CGFontGetGlyphAdvances to compute the size yourself.
Scott_______________________________________________
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