• 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: Drawing text?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >Drawing text? (From: Ernst Persson <email@hidden>)

  • Prev by Date: Re: How to get file creation date
  • Next by Date: Re: OT: Categories and alloc.
  • Previous by thread: Drawing text?
  • Next by thread: Re: Drawing text?
  • Index(es):
    • Date
    • Thread