How to measure the width of text?
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.