Re: Drawing text with specifying font
Re: Drawing text with specifying font
- Subject: Re: Drawing text with specifying font
- From: Greg Titus <email@hidden>
- Date: Fri, 6 Jul 2001 12:57:41 -0700
On Friday, July 6, 2001, at 10:52 AM, Candide Kemmler wrote:
>
Hi !
>
>
I'm trying to draw text with various transformations and a font.
>
The transformations work, but I can't specify the font.
>
I tried [ NSFont set ] (you know what I mean), but when I do
>
[NSString drawAtPoint:p withDictionary:nil ],
>
the font remains to a default value.
>
Perhaps I should consider the dictionary to be provided. But in the
>
absence of any doc, I just set it to nil.
>
Does anyone know how to handle this ?
Hi Candide,
You do indeed need to set the dictionary. The dictionary is the set of
attributes to use when drawing the string - these are the same
attributes as in an NSAttributedString, so check the documentation and
header files for NSAttributedString. Note that there are two sets: the
class itself is defined in Foundation, but the specific attributes for
displaying on-screen are in AppKit.
What you want is to create a dictionary with the settings you are
interested in:
NSMutableDictionary *attributes;
attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSFont fontWithName:@"Some Font" size:12.0]
forKey:NSFontAttributeName];
[attributes setObject:[NSColor blueColor]
forKey:NSForegroundColorAttributeName];
...and so on...
And then use that dictionary for drawing each of your strings:
[string drawAtPoint:p withDictionary:attributes];
>
PS: it seems to me that specifying a dictionary at every drawString is
>
a huge overhead...
It turns out to be not so bad because you generally set up the
dictionary once for each style of text you want to draw and then reuse
the dictionary for each string in that style. I would hope, though I
haven't specifically verified it, that the drawAtPoint:withDictionary:
method checks to see if things like the font and text color passed to it
are the same as those already set in the drawing context so that there
is a very minimum amount of overhead.
Hope this helps,
--Greg