Re: drawing a simple string with NSFont
Re: drawing a simple string with NSFont
- Subject: Re: drawing a simple string with NSFont
- From: Douglas Davidson <email@hidden>
- Date: Mon, 13 Aug 2007 16:35:25 -0700
On Aug 7, 2007, at 2:29 AM, Scott F Bayes wrote:
I'm trying to draw an NSString on a NSView while in its drawRect,
using
- (void)drawInRect:(NSRect)aRect withAttributes:(NSDictionary *)
attribute
from the AppKit NSString additions. I can draw the string where I
want to without problem, but I want to select a font and a font
size. "Simple Text Drawing" in the Drawing Guide says:
"For an NSString object, you can apply basic attributes (such as
font, color, and style settings) to the entire string during
drawing." Perfect!
It sounds like setting font&size can be accomplished either with
the NSDictionary *withAttributes for drawInRect above, or by
[myFont set] while in drawRect.
I find I can create what appears to be a valid NSFont, no problem.
But [myFont set] in drawRect doesn't appear to do anything, and I
don't know how to construct the NSDictionary mentioned above,
assuming it's what I need; I can't figure out what key to use.
Documentation has led me through enough twists and turns that I'm
now thoroughly confused.
Any advice or a short example would be appreciated.
The critical thing to remember here is that the Cocoa text system
always takes as much as possible from the attribute dictionaries
rather than from the drawing context, whether the attributes are
present or not. Setting a font or color on the context will
potentially affect other sorts of drawing, but not Cocoa text. The
Cocoa text system will always use the value of NSFontAttributeName,
or NSForegroundColorAttributeName, or any of the other text
attributes from AppKit/NSAttributedString.h; or if any attribute is
not present then a default value will be used, as specified in that
header.
Douglas Davidson
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden