RE: How to measure the width of text?
RE: How to measure the width of text?
- Subject: RE: How to measure the width of text?
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Tue, 9 Dec 2003 10:05:06 -0500
Check the AppKit docs for NSString Additions. You'll
find -sizeWithAttributes: there.
Jonathan
>
-----Original Message-----
>
From: email@hidden
>
[mailto:email@hidden]On Behalf Of Michael Bonnice
>
Sent: Monday, December 08, 2003 10:06 PM
>
To: email@hidden
>
Subject: How to measure the width of text?
>
>
>
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.
_______________________________________________
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.