views:

42

answers:

3

Title says all. :) Looking for essentially whatever the Cocoa equivalent of [UILabel adjustsFontSizeToFitWidth] is.

A: 

I used Jerry Krinock's excellent NS(Attributed)String+Geometrics (located here) and a small method like the following. I'm still interested in a simpler way.

- (void) prepTextField:(NSTextField *)field withString:(NSString *)string
{   
    #define kMaxFontSize 32.0f
    #define kMinFontSize 6.0f
    float fontSize = kMaxFontSize;
    while (([string widthForHeight:[field frame].size.height font:[NSFont systemFontOfSize:fontSize]] > [field frame].size.width) && (fontSize > kMinFontSize))
    {
            fontSize--;
    }
    [field setFont:[NSFont systemFontOfSize:fontSize]];

    [field setStringValue:string];

    [self addSubview:field];
}
refulgentis
A: 

Don't forget to look in superclasses. An NSTextField is a kind of NSControl, and every NSControl responds to the sizeToFit message.

Peter Hosey
+1  A: 

I answered this for someone else. You can see my answer here.

regulus6633