Usually, while working with Cocoa, you won't need to mess around with RTF text. In case you do, however, you are going to ned a few explanations n how you can implement RTF text in your application.

First of all, NSString is text comprised of unichars, which are Unicode characters (not ASCII, as used in plain C). An RTF text is comprised of an NSString, in which information about every character and its appearance have been stored. Therefore, the NSString class is not enough to handle an RTF Unicode text, The class you are looking for in NSAttributedString and NSMutableAttributedString , which in fact exists to manipulate a document or an RTF string.

A way to create an attributed string is with the following command:

(id)initWithString:(NSString *)aString

και θα δημιουργηθεί ένα string χωρίς πληροφορίες για οτιδήποτε άλλο. Αλλιώς, μπορείτε να χρησιμοποιήσετε την εντολή:

– (id)initWithString:(NSString *)aString attributes:(NSDictionary *)attributes

Which will give you an rtf string with properties that correspond to those defined in the Dictionary. Those are the basic functions provided by the Foundation Framework for this class. However, there are routines that expand functionality of  NSAttributedStrings and allows the programmer to use an rtf text as argument. Those are the following (only existing in the Cocoa Framework).


They are self-explained. Although these functions are provided,you will rarely need to mess around programmatically with this class if youwant to save to or load from a file from your hard disk. The classes that will really come in handy in this situation is NStext and NSData . Let's say that you have an NSTextView object ad you want to fill it with a RTF contents contained in a file.The text view's programmatic name is "body". The following lines load a file from the location defined by pathLocation and fille the textView with the recently loaded rtf text.

NSData *data = [NSData dataWithContentsOfFile:pathLocation];
body = [[NSAttributedString alloc]initWithRTF:data documentAttributes:nil];

And the following lines save a file with the contents of the above-defined NSTextView in the location defined by "path".

NSData *rtfData = [body RTFFromRange:NSMakeRange(0,[[documentBody textStorage]length])];
[rtfData writeToFile:path atomically:NO];

As you can see, NSAttributedString was not used at all in both cases. When working with Cocoa Bindings, however, you will need to allocate objects of some classes, and the primary variable that's going to be used in those classes are of NSAttributedString class. In this case, you can allocate an example attributed string with the following lines:

NSFont *font = [NSFont fontWithName:@"Verdana" size:12.0f];
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:font forKey:NSFontAttributeName];
[attributes setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
NSAttributedString *str = [[NSAttributedString alloc]initWithString:@"Hello world" attributes:attributes];

Lastly, when you wish to associate an object from an interface to a CoCoa Bindings object, if the attribute of the class with which you associate it is an NSAttibutedString, you must connect the property "Attributed String" and not "data" as you were doing in the case of a plain NString. If you want to connect the "data" property anyway, you will need to firstly convert the attributed string into an NSData object and then use it. But I don't see why anywone would want to do this.

For more information, it would be good to visit those locations and learn more about Cocoa's text system.
Apple's Text System OverView