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
2010-07-24 08:25:27
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
2010-07-24 16:30:24