NSText sizeToFit doesn't
NSText sizeToFit doesn't
- Subject: NSText sizeToFit doesn't
- From: Wade Tregaskis <email@hidden>
- Date: Sun, 4 Jan 2004 00:47:04 +1100
The following code, from an NSView subclass:
- (void)addLabel:(NSString*)label text:(NSString*)text {
float fontSize = [NSFont labelFontSize];
NSFont *labelFont = [NSFont boldSystemFontOfSize:fontSize],
*textFont = [NSFont labelFontOfSize:fontSize];
static NSSize minSize = {0, 0};
NSText *labelView = nil, *textView = nil;
labelView = [[[NSText alloc] initWithFrame:NSMakeRect(0,
currentPosition, 150, space)] autorelease];
[labelView setFont:labelFont];
[labelView setString:label];
[labelView setMinSize:minSize];
[labelView setHorizontallyResizable:YES];
[labelView setVerticallyResizable:NO];
[labelView sizeToFit];
[labelView setEditable:NO];
[labelView setUsesFontPanel:NO];
[labelView setDrawsBackground:NO];
[self addSubview:labelView];
...
}
The problem is that for some text sizeToFit reduces the size too much,
so that the last character is forced onto the next line. One simple
example is the string @"Email". One workaround I've found is to set
the minimum width to 10 + ceil([labelFont widthOfString:label]), but
apart from the fact that the 10 is an arbitrary and meaningless
constant (probably dependent on the font and/or size), widthOfString is
depreciated. The NSString additions that replace it aren't suitable
for this use.
I don't know why I have to add 10 to the width. Anything less doesn't
fix it, anything equal to or above does. Strangely, adding 10 (and no
more) doesn't change the spacing for the working strings from without
it. So it's a "perfect" workaround in my little sandbox usage.
The problem still remains, nonetheless, that sizeToFit doesn't. Anyone
have any idea why, or at least a "proper" way to make it work? I've
only seen the problem with the bold system font as yet - presumably
that's a key.
Wade Tregaskis
-- Sed quis custodiet ipsos custodes?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.