• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSText sizeToFit doesn't
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSText sizeToFit doesn't


  • Subject: Re: NSText sizeToFit doesn't
  • From: Adrian Bool <email@hidden>
  • Date: Sat, 3 Jan 2004 15:23:35 +0100

Hi Wade,

I can't see a [labelFont set] in your code so that labelView kows what font to use for it's size calculations. (I'm guessing that it is calculating the size based upon a non-bold font variant and you are then setting the font to bold (larger) just before you draw...)

Cheers,

aid


On 3 Jan 2004, at 14:47, Wade Tregaskis wrote:

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.
_______________________________________________
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.

  • Follow-Ups:
    • Re: NSText sizeToFit doesn't
      • From: Wade Tregaskis <email@hidden>
References: 
 >NSText sizeToFit doesn't (From: Wade Tregaskis <email@hidden>)

  • Prev by Date: Re: Encryption
  • Next by Date: object_getInstanceVariable() and non atomic var types
  • Previous by thread: NSText sizeToFit doesn't
  • Next by thread: Re: NSText sizeToFit doesn't
  • Index(es):
    • Date
    • Thread