Return to the Alphabetic Index
Return to the Class Browser
Return to the Picture Browser
Copyright (c) 1994 by NeXT Computer, Inc. All Rights Reserved.

NSCStringText

Inherits From: NSText : NSView : NSResponder : NSObject

Conforms To: NSChangeSpelling, NSIgnoreMisspelledWords (NSText) NSCoding (NSResponder)

NSObject (NSObject)

Declared In: AppKit/NSCStringText.h

Class Description

The NSCStringText class declares the programmatic interface to objects that manage text using eight-bit character encodings. The encoding is the same as the default C string encoding provided by defaultCStringEncoding in the NSString class. NSCStringText can be used in situations where backwards compatibility with the detailed interfaces of the NEXTSTEP Text object is important. Applications that can use the interface of NSText should do so.

The NSCStringText class is unlike most other classes in the Application Kit in its complexity and range of features. One of its design goals is to provide a comprehensive set of text-handling features so that you'll rarely need to create a subclass. An NSCStringText object can (among other things):

. Control the color of its text and background.

. Control the font and layout characteristics of its text.

. Control whether text is editable.

. Wrap text on a word or character basis.

. Write text to, or read it from, a file, as either RTF or plain ASCII data.

. Display graphic images within its text.

. Communicate with other applications through the Services menu.

. Let another object, the delegate, dynamically control its properties.

. Let the user copy and paste text within and between applications.

. Let the user copy and paste font and format information between NSCStringText objects.

. Let the user check the spelling of words in its text.

. Let the user control the format of paragraphs by manipulating a ruler.

NSCStringText can deal only with eight-bit characters. Therefore, it is not able to deal with Unicode character sets, and NSCStringText can't be fully internationalized.

Plain and Rich NSCStringText Objects

When you create an NSCStringText object directly, by default it allows only one font, line height, text color, and paragraph format for the entire text. You can set the default font used by new NSCStringText instances by sending the NSCStringText class object a setDefaultFont: message. Once an NSCStringText object is created, you can alter its global settings using methods such as setFont:, setLineHeight:, setTextGray:, and setAlignment:. For convenience, such an NSCStringText object will be called a plain NSCStringText object.

To allow multiple values for these attributes, you must send the NSCStringText object a setRichText:YES message. An NSCStringText object that allows multiple fonts also allows multiple paragraph formats, line heights, and so on. For convenience, such an NSCStringText object will be called a rich NSCStringText object.

A rich NSCStringText object can use RTF (Rich Text Format) as an interchange format. Not all RTF control words are supported: On input, an NSCStringText object ignores any control word it doesn't recognize; some of those it can read and interpret it doesn't write out. Refer to the class description of NSText for a list of the RTF control words that an NSCStringText object recognizes.

Note: An NSCStringText object writes eight-bit characters in the default C string encoding, which differs somewhat from the ANSI character set.

In an NSCStringText object, each sequence of characters having the same attributes is called a run. A plain NSCStringText object has only one run for the entire text. A rich NSCStringText object can have multiple runs. Methods such as setSelFont: and setSelColor: let you programmatically modify the attributes of the selected sequence of characters in a rich NSCStringText object. As discussed below, the user can set these attributes using the Font panel and the ruler.

NSCStringText objects are designed to work closely with various objects and services. Some of thesesuch as the delegate or an embedded graphic objectrequire a degree of programming on your part. Otherssuch as the Font panel, spelling checker, ruler, and Services menutake no effort other than deciding whether the service should be enabled or disabled. The following sections discuss these interrelationships.

Notifying the NSCStringText Object's Delegate

Many of an NSCStringText object's actions can be controlled through an associated object, the NSCStringText object's delegate. If it implements any of the following methods, the delegate receives the corresponding message at the appropriate time:

textWillResize:

textDidResize:oldBounds:

textWillSetSel:toFont:

textWillConvert:fromFont:toFont:

textWillStartReadingRichText:

textWillFinishReadingRichText:

textWillWrite:

textDidRead:paperSize:

So, for example, if the delegate implements the textWillConvert:fromFont:toFont: method, it will receive notification upon the user's first attempt to change the font of the text. Moreover, depending on the method's return value, the delegate can either allow or prohibit changes to the text. See Methods Implemented by the Delegate. The delegate can be any object you choose, and one delegate can control multiple NSCStringText objects.

Adding Graphics to the Text

A rich NSCStringText object allows graphics to be embedded in the text. Each graphic is treated as a single (possibly large) character: The text's line height and character placement are adjusted to accommodate the graphic character. Graphics are embedded in the text in either of two ways: programmatically or directly through user actions. The programmatic approach is discussed first.

In the programmatic approach, you add an objectgenerally a subclass of NSCellto the text. This object manages the graphic image by drawing it when appropriate. Although NSCell subclasses are commonly used, the only requirement is that the embedded object responds to these messagessee Methods Implemented by an Embedded Graphic Object for more information:

highlight:withFrame:inView:

drawWithFrame:inView:

trackMouse:inRect:ofView:untilMouseUp:

cellSize:

readRichText:forView:

richTextforView:

You place the graphic object in the text by sending the NSCStringText object a replaceSelWithCell: message.

An NSCStringText object displays a graphic in its text by sending the managing object a drawWithFrame:inView: message. To record the graphic to a file or to the pasteboard, the NSCStringText object sends the managing object a richTextforView: message. The object must then write an RTF control word along with any data (such as the path of a TIFF file containing its image data) it might need to recreate its image. To reestablish the text containing the graphic image from RTF data, an NSCStringText object must know which class to associate with particular RTF control words. You associate a control word with a class object by sending the NSCStringText class object a registerDirective:forClass: message. Thereafter, whenever an NSCStringText object finds the registered control word in the RTF data being read from a file or the pasteboard, it will create a new instance of the class and send the object a readRichText:forView: message.

An alternate means of adding an image to the text is for the user to drag an EPS or TIFF file icon directly into an NSCStringText object. The NSCStringText object automatically creates a graphic object to manage the display of the image. This feature requires a rich NSCStringText object that has been configured to receive dragged imagessee the setImportsGraphics: method of the NSText class.

Images that have been imported in this way can be written as RTFD documents. Programmatic creation of RTFD documents is not supported in this version of OpenStep. RTFD documents use a file package, or directory, to store the components of the document (the D stands for directory). The file package has the name of the document plus a .rtfd extension. The file package always contains a file called TXT.rtf for the text of the document, and one or more TIFF or EPS files for the images. An NSCStringText object can transfer information in an RTFD document to a file and read it from a filesee the writeRTFDToFile:atomically: and readRTFDFromFile: methods in the NSText methods.

Cooperating with Other Objects and Services

NSCStringText objects are designed to work with the Application Kit's font conversion system. By default, an NSCStringText object keeps the Font panel updated with the font of the current selection. It also changes the font of the selection (for a rich NSCStringText object) or of the entire text (for a default NSCStringText object) to reflect the user's choices in the Font panel or menu. To disconnect an NSCStringText object from this service, send it a setUsesFontPanel:NO message (this method is actually implemented by NSTextthe superclass).

If an NSCStringText object is a subview of an NSScrollView, it can cooperate with the NSScrollView to display and update a ruler that displays formatting information. The NSScrollView retiles its subviews to make room for the ruler, and the NSCStringText object updates the ruler with the format information of the paragraph containing the selection. The toggleRuler: method controls the display of this ruler. Users can modify paragraph formats by manipulating the components of the ruler.

By means of the Services menu, an NSCStringText object can make use of facilities outside the scope of its own application. By default, an NSCStringText object registers with the services system that it can send and receive RTF and plain ASCII data. If the application containing the NSCStringText object has a Services menu, a menu item is added for each service provider that can accept or return these formats. To prevent NSCStringText objects from registering for services, send the NSCStringText class object an excludeFromServicesMenu:YES message before any NSCStringText objects are created.

Coordinates and sizes mentioned in the method descriptions below are in PostScript units1/72 of an inch.

Initializing a New NSCStringText Object

Modifying the Frame Rectangle

Laying Out the Text

Reporting Line and Position

Reading and Writing Text

Editing Text

Managing the Selection

Setting the Font

+ (NSFont *)defaultFont Returns the default NSFont object for NSCStringText objects.

+ (void)setDefaultFont:(NSFont *)anObject Makes anObject the default NSFont object for NSCStringText objects.

- (void)setFont:(NSFont *)fontObj Sets the NSFont object and paragraph style for all text.

paragraphStyle:(void *)paragraphStyle

- (void)setSelFont:(NSFont *)fontObj Sets the NSFont object for the selection.

- (void)setSelFont:(NSFont *)fontObj Sets the NSFont object and paragraph style for the

paragraphStyle:(void *)paragraphStyle selection.

- (void)setSelFontFamily:(NSString *)fontName Sets the font family for the selection.

- (void)setSelFontSize:(float)size Sets the font size for the selection.

- (void)setSelFontStyle:(NSFontTraitMask)traits Sets the font style for the selection.

Finding Text

Modifying Graphic Attributes

Reusing an NSCStringText Object

Setting Window Attributes

Assigning a Tag

Handling Event Messages

Displaying Graphics within the Text

Setting Tables and Functions

Printing

Implemented by the Delegate

Compatibility Methods