views:

304

answers:

3

I have a view that contains two NSTextFieldCells. The size at which these cells are drawn is derived from the size of the view, and I want the text in each cell to be the largest that will fit in the derived size of the cell. Here's what I have, which doesn't set the font size:

- (void)drawRect:(NSRect)dirtyRect {
    /*
     * Observant readers will notice that I update the whole view here. If
     * there is a perceived performance problem, then I'll switch to just
     * updating the dirty rect.
     */
    NSRect boundsRect = self.bounds;
    const CGFloat monthHeight = 0.25 * boundsRect.size.height;
    NSRect monthRect = NSMakeRect(boundsRect.origin.x,
                                  boundsRect.origin.y + boundsRect.size.height
                                  - monthHeight,
                                  boundsRect.size.width,
                                  monthHeight);
    [monthCell drawWithFrame: monthRect inView: self];

    NSRect dayRect = NSMakeRect(boundsRect.origin.x,
                                boundsRect.origin.y,
                                boundsRect.size.width,
                                boundsRect.size.height - monthHeight);
    [dayCell drawWithFrame: dayRect inView: self];

    [[NSColor blackColor] set];
    [NSBezierPath strokeRect: boundsRect];
}

So I know that I can ask a string what size it would take for given attributes, and I know that I can ask a control to change its size to fit its content. Neither of those seems applicable: I want the content (in this case, the cell's stringValue) to size to fit the known rect dimensions, with the attributes needed to achieve that being unknown. How can I find the needed size? Assume that I know what font I'll be using (because I do).

update Note: I don't want to truncate the string, I want to grow or shrink it so that the whole thing fits, with the largest text size possible, into the provided rect.

A: 

Two ideas. One I've tried, the other might work:

1) Do like in this question: http://stackoverflow.com/questions/2266396/how-to-truncate-an-nsstring-based-on-the-graphical-width i.e. just try out different sizes until it doesn't fit anymore

2) Create the cell, give it the maximum rect and set it to fit its text into the cell, then ask it for its ideal size (there's a method on there that does that) then resize the cells again. At last if I understood your problem correctly.

uliwitness
@uliwitness: thanks, but I must have misrepresented my goal. I've clarified the question: I don't want to truncate the text, I want to choose an appropriate font size.
Graham Lee
A: 

It was recommended out of band that I try a binary search for an appropriate size. This is a very limited example of that:

- (NSFont *)labelFontForText: (NSString *)text inRect: (NSRect)rect {
    CGFloat prevSize = 0.0, guessSize = 16.0, tempSize;
    NSFont *guessFont = nil;
    while (fabs(guessSize - prevSize) > 0.125) {
        guessFont = [NSFont labelFontOfSize: guessSize];
        NSSize textSize = [text sizeWithAttributes: 
                            [NSDictionary dictionaryWithObject: guessFont
                                                        forKey: NSFontAttributeName]];
        if (textSize.width > rect.size.width || 
            textSize.height > rect.size.height) {
            tempSize = guessSize - (guessSize - prevSize) / 2.0;
        }
        else {
            tempSize = guessSize + (guessSize - prevSize) / 2.0;
        }
        prevSize = guessSize;
        guessSize = tempSize;
    }
    return [[guessFont retain] autorelease];
}

The limitations (you'd better not need a 32pt or larger font, or anything that ain't Lucida Grande) are not important for my need, but certainly would put some people off using this method. I'll leave the question open, and accept a more robust approach.

Graham Lee
+2  A: 

I use some similar code but it handles different fonts, sizes up to 10,000 and takes into account the available height as well as width of the area the text is being displayed in.

#define kMaxFontSize    10000

- (CGFloat)fontSizeForAreaSize:(NSSize)areaSize withString:(NSString *)stringToSize usingFont:(NSString *)fontName;
{
    NSFont * displayFont = nil;
    NSSize stringSize = NSZeroSize;
    NSMutableDictionary * fontAttributes = [[NSMutableDictionary alloc] init];

    if (areaSize.width == 0.0 && areaSize.height == 0.0)
        return 0.0;

    NSUInteger fontLoop = 0;
    for (fontLoop = 1; fontLoop <= kMaxFontSize; fontLoop++) {
        displayFont = [[NSFontManager sharedFontManager] convertWeight:YES ofFont:[NSFont fontWithName:fontName size:fontLoop]];
        [fontAttributes setObject:displayFont forKey:NSFontAttributeName];
        stringSize = [stringToSize sizeWithAttributes:fontAttributes];

        if (stringSize.width > areaSize.width)
            break;
        if (stringSize.height > areaSize.height)
            break;
    }

    [fontAttributes release], fontAttributes = nil;

    return (CGFloat)fontLoop - 1.0;
}
sgaw
Thanks, I like that approach. I'd be inclined to remove the 10k limit and aim for "when it's done, it's done" - it won't change the behaviour in almost any case.
Graham Lee