Re: Drawing text?
Re: Drawing text?
- Subject: Re: Drawing text?
- From: Cameron Hayne <email@hidden>
- Date: Mon, 8 Jul 2002 21:12:02 -0400
On Monday, July 8, 2002, at 08:49 pm, Ernst Persson wrote:
I'm working on a game (with Obj-C, Cocoa) and.....
I can't figure out how to draw text in my NSView!
What's the simple way to draw an NSString to a custom NSView (in the
drawRect method)?
Here's a function that draws a string centred in the rect (to be called
from drawRect):
static void drawStringInRect(NSRect rect, NSString *str, int fontSize)
{
NSDictionary *dict = [NSDictionary
dictionaryWithObject: [NSFont
boldSystemFontOfSize: fontSize]
forKey: NSFontAttributeName];
NSAttributedString *astr = [[NSAttributedString alloc]
initWithString: str
attributes: dict];
NSSize strSize = [astr size];
NSPoint pt = NSMakePoint((rect.size.width - strSize.width) / 2,
(rect.size.height - strSize.height) / 2);
// clear the rect
rect.origin.x = 0;
rect.origin.y = 0;
[[NSColor whiteColor] set];
NSRectFill(rect);
// draw the string
[astr drawAtPoint:pt];
}
--
Cameron Hayne (email@hidden)
Hayne of Tintagel
_______________________________________________
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.