Re: A Thousand Newbie Questions...
Re: A Thousand Newbie Questions...
- Subject: Re: A Thousand Newbie Questions...
- From: Charles Jolley <email@hidden>
- Date: Mon, 8 Oct 2001 01:39:54 -0500
OK, there are several ways to set the color text in your document. All
of them require you understand how rich text is displayed. Text is
stored in an attributed string. This is an object that not only holds a
string, but it also lets you assign attributes to ranges in the string.
These attributes can really be anything (they work like a dictionary),
though the Cocoa text system has a specific set it understands. There
are two attributes for setting the color. The key you use to access
them are:
NSForegroundColorAttributeName
NSBackgroundColorAttributeName
There are two places you can work on the attributes for your text. One
is to modify the text of your document itself. Text for a text view is
stored in an NSTextStorage object, which is an enhanced
NSMutableAttributedString. You can get this from the text view using
the -textStorage method.
Using this approach, you could make the first five characters of your
document red by the following:
[[myTextView textStorage] setAttribute: NSForegroundColorAttributeName
value: [NSColor redColor] range: NSMakeRange(0,5)] ;
Ok, but there is an even better way. The text storage object is for
storing your document contents itself. Cocoa actually has a special
method for setting temporary attributes that are for display use only.
You will find this in the NSLayoutManager, obtained from the text view
using -layoutManager. This is a little more complex because you will
have to construct the attribute dictionary yourself:
NSDictionary* myAttrs = [NSDictionary dictionaryWithObject: [NSColor
recColor] forKey: NSForegroundColorAttributeName] ;
[[myTextView layoutManager] addTemporaryAttributes: myAttrs
forCharacterRange: NSMakeRange(0,5)] ;
Honestly, either way will probably work for you because when you save
source files, you will probably ignore any attributes anyway since all
you are interested in is the text. Nevertheless, I would recommend the
second method because it more closely fits with the intentions of those
who designed the Cocoa text system.
HTH,
-Charles
PS: one potential gotcha you might run into: to do anything with the
text storage, the text system must be activated to support rich text.
If this is a problem, just send setRichText: YES to your text view.
On Sunday, October 7, 2001, at 11:56 PM, email@hidden wrote:
I really just want to be something that would be useful, and, I think
this would be, considering one could extend the DTD to include
documentation, attributes, and examples...making it a universally cool
code editor, open-sourced and free to the world. This seems like would
take a maximum of 50-or-so lines of code initially, but, god, Cocoa
docs are really sparse, and all the tutorials right now are fairly
entry level...
Anyway, any help is greatly appreciated, and I promise to sing a
heartfelt rendition of Amy Grant's 'Baby Baby' to all good knights who
could help.
Thanks In Advance,
Jack Shedd