Question

So I have an NSFont, and I want to get the maximum dimensions for any characters, ie. the pitch and letter height. [font maximumAdvancement] seems to return an NSSize of {pitch, 0}, so that's not helping. Bounding rect doesn't seem to work either, and the suggestion from jwz's similar question of creating a bezier path, appending a glyph and getting the bounding rectange is also giving me back {0, 0}. What gives here?

UPDATE: The code I'm using to get the bezier size is this:

NSBezierPath *bezier = [NSBezierPath bezierPath];
NSGlyph g;
{
    NSTextStorage *ts = [[NSTextStorage alloc] initWithString:@" "];
    [ts setFont:font];
    NSLayoutManager *lm = [[NSLayoutManager alloc] init];
    NSTextContainer *tc = [[NSTextContainer alloc] init];
    [lm addTextContainer:tc];
    [tc release]; // lm retains tc
    [ts addLayoutManager:lm];
    [lm release]; // ts retains lm
    g = [lm glyphAtIndex:0];
    [ts release];
}
NSPoint pt = {0.0f};
[bezier moveToPoint:pt];
[bezier appendBezierPathWithGlyph:g inFont:font];
NSRect bounds = [bezier bounds];
Was it helpful?

Solution

The glyph for the space character doesn't have any subpaths, so of course its bounds have size NSZeroSize. Try -[NSFont boundingRectForFont] instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top