• 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
Re: NSString drawInRect:withAttributes: clips descenders
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
References: 
 >NSString drawInRect:withAttributes: clips descenders (From: Brant Sears <email@hidden>)

  • Prev by Date: Re: CoreData, NSArrayController
  • Next by Date: Re: "Send to iWeb" API?
  • Previous by thread: Re: NSString drawInRect:withAttributes: clips descenders
  • Next by thread: Problem with prebinding and Frameworks
  • Index(es):
    • Date
    • Thread