• 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
Padding around a borderless NSTextField
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Padding around a borderless NSTextField


  • Subject: Padding around a borderless NSTextField
  • From: email@hidden (Stefan Haller)
  • Date: Tue, 6 Sep 2005 20:20:16 +0200
  • Organization: Ableton

Consider this code:

  NSTextField* myTextField;

  // ...

  [myTextField setBordered:NO];
  [myTextField sizeToFit];
  NSSize frameSize = [myTextField frame].size;
  NSSize stringSize = [myTextField attributedStringValue] size];

Then frameSize is 4 pixels wider than stringSize (but the same height).
Is there any way to determine this "4" value at runtime, rather than
hard-coding it?

And then, if I try to calculate the attributedString's size myself using
an ad-hoc NSLayoutManager (see code below), I get a size that is the
same width as stringSize, but height is 1 pixel less than that of
stringSize.  Again, what accounts for this?  And is the assumption about
the lineFragmentPadding valid in the code below?



  NSSize drawingSizeForAttributedString(NSAttributedString* str)
  {
    assert([str length] > 0);

    NSTextStorage* textStorage = [[[NSTextStorage alloc]
      initWithAttributedString:str] autorelease];
    NSLayoutManager* layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];
    [layoutManager release];

    NSTextContainer* textContainer = [[NSTextContainer alloc]
      initWithContainerSize:NSMakeSize(1.e7, 1.e7)];
    [textContainer setLineFragmentPadding:0];
    [layoutManager addTextContainer:textContainer];
    [textContainer release];

    NSRange glyphRange = [layoutManager
      glyphRangeForCharacterRange:NSMakeRange(0, [str length])
      actualCharacterRange:NULL];

    // Force glyph layout:
    (void)[layoutManager textContainerForGlyphAtIndex:
      NSMaxRange(glyphRange) - 1 effectiveRange:NULL];

    NSRect rect = [layoutManager
      usedRectForTextContainer:textContainer];

    return rect.size;
  }
}



--
Stefan Haller
Ableton
http://www.ableton.com/
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: Programatically create URLs in NSTextView or ??
  • Next by Date: Re: Programatically create URLs in NSTextView or ??
  • Previous by thread: Re: Core-Data & validate<key>:error:
  • Next by thread: Strange bug (?) with NSArrayController sortDescriptors and SQLite !
  • Index(es):
    • Date
    • Thread