• 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
How to measure the width of text?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to measure the width of text?


  • Subject: How to measure the width of text?
  • From: Michael Bonnice <email@hidden>
  • Date: Mon, 8 Dec 2003 20:05:57 -0700

How can I measure the width of a text string when drawn, or
(alternately) know what is the x coordinate just to the right of the
right-most pixel in a string?

Here is how I am displaying the string:

@interface TunerYAxisView : NSView
{
NSMutableDictionary *textAttributes; // attributes of the text
labels
float fontHeight; // height of font, it might get scrunched
}

...

- (void)drawRect:(NSRect)rect
{
// set up the text attributes
NSFont *font = [NSFont systemFontOfSize:fontHeight];
textAttributes = [NSMutableDictionary dictionary];
[textAttributes setObject:font forKey:NSFontAttributeName];
[textAttributes setObject:[NSColor blackColor]
forKey:NSForegroundColorAttributeName];
float space = NSHeight([self frame]) / 12.0;
float xLoc = NSMidX([self frame]);
float offset = fontHeight/2.0 * 1.2; // offset the font so it
is roughly centered on the line
[@"A" drawAtPoint:NSMakePoint(xLoc, 5.0 * space - offset )
withAttributes:textAttributes];

}

What I'd really like to do is right-justify the text or center the text.

Mike
_______________________________________________
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: How to measure the width of text?
      • From: "Jonathan E. Jackel" <email@hidden>
    • Re: How to measure the width of text?
      • From: mmalcolm crawford <email@hidden>
  • Prev by Date: tiff from NSImage, at 300 dpi
  • Next by Date: Re: tiff from NSImage, at 300 dpi
  • Previous by thread: Re: tiff from NSImage, at 300 dpi
  • Next by thread: Re: How to measure the width of text?
  • Index(es):
    • Date
    • Thread