Re: NSString drawInRect:withAttributes: clips descenders
Re: NSString drawInRect:withAttributes: clips descenders
- Subject: Re: NSString drawInRect:withAttributes: clips descenders
- From: "Michael J. Bergin" <email@hidden>
- Date: Thu, 4 May 2006 11:48:35 -0400
Use NSString's drawAtPoint:withAttributes to get the correct
positioning. Here's an example using an NSView:
(Create an NSView subclass and paste this method into the
implementation file to see it work.)
- (void)drawRect:(NSRect)aRect
{
NSLayoutManager *lm;
NSFont *font;
NSRect bounds;
lm = [[NSLayoutManager alloc] init];
font = [NSFont fontWithName:@"Ayuthaya" size:18];
bounds = [self bounds];
bounds.size.height = [lm defaultLineHeightForFont:font];
[self setFrame:[self convertRect:bounds toView:[self superview]]];
[NSGraphicsContext saveGraphicsState];
[[NSColor blueColor] set];
[NSBezierPath strokeRect:bounds];
[@"jJpP" drawAtPoint:NSMakePoint(0, 0) withAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,
nil]];
[NSGraphicsContext restoreGraphicsState];
}
-
Michael J. Bergin
email@hidden
Odology, inc.
www.odology.com
Launch any app, anywhere, in under a second
Launcher by Odology
www.odology.com/launcher
On May 3, 2006, at 1:55 PM, Brant Sears wrote:
Hi. I'm trying to use the method drawInRect: withAttributes: method
of NSString to draw a single character at a time into an NSImage.
The problem that I am having is that descenders are being clipped.
So, for example, the character j is being chopped where it descends
below the baseline. This affects other descenders like p, q, y, etc.
What can I do to fix this? It seems like this call sets the
baseline at the very bottom of the NSRect that is specified in the
method.
The method that I am using is as follows:
NSFont * myFont = [NSFont fontWithName:@"Monaco" size:10];
NSDictionary * attsDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor whiteColor], NSForegroundColorAttributeName,
myFont, NSFontAttributeName,
[NSNumber numberWithInt:NSNoUnderlineStyle],
NSUnderlineStyleAttributeName,
nil ];
[charStr drawInRect: charRect withAttributes:attsDict];
The context in which I am doing this is that the NSImage has
already been locked into focus and had its background filled with
black. Is there an attribute I can use to raise the baseline? Also,
I have to have this work on 10.2 and up. A 10.4 only solution can
be used, but I would still need some other solution for 10.2/10.3.
Thanks!
Brant Sears
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40odology.com
This email sent to email@hidden
_______________________________________________
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