Re: String color
Re: String color
- Subject: Re: String color
- From: Douglas Davidson <email@hidden>
- Date: Wed, 19 Sep 2001 13:51:21 -0700
On Wednesday, September 19, 2001, at 11:38 AM, Mark Wridt wrote:
I cannot figure out or find code to change a string's
color.
I have a color set, but getting the string to be that
color is causing errors (mostly selector not
recognized)
My color code (that is working):
NSColor *color;
color = [[NSColor redColor] retain];
but
[aString setColor:color];
[aString textColor:color];
[aString setTextColor];
(as well as other crazy combinations)
all are not working.
Strings do not have color. Strings are conceptually sequences of
Unicode characters, i.e. plain text.
Attributed strings may have color. Attributed strings are strings with
arbitrary attributes attached to the characters, i.e. rich text.
NSAttributedString is defined in Foundation, and by itself it does not
specify what the attributes may be. The AppKit then defines a number of
attributes that it considers interesting, in the AppKit's header
NSAttributedString.h, and among them is NSForegroundColorAttributeName,
used to specify the color of the text.
One thing that sometimes confuses people is that such things as [color
set] do not affect string drawing. Methods like -[NSString
drawInRect:withAttributes:] and -[NSAttributedString drawInRect:] take
attributes such as color from attribute dictionaries--in the first case,
from the attribute dictionary supplied as the second argument, and in
the second case, from the attributes of the attributed string.
You want to either create an attributed string from your string, and add
your color as an attribute to it, or else (if you are using -[NSString
drawInRect:withAttributes:] and the like) supply your color as an
attribute in the attribute dictionary, under the key
NSForegroundColorAttributeName.
Douglas Davidson