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.


Inherits From: NSView : NSResponder : NSObject

Conforms To: NSChangeSpelling, NSIgnoreMisspelledWords NSCoding (NSResponder)

NSObject (NSObject)

Declared In: AppKit/NSTextView.h

Class Description

The NSText class declares the programmatic interface to objects that manage text. NSText objects are used by the Application Kit wherever text appears in interface objects: An NSText object draws the title of a window, the commands in a menu, the title of a button, and the items in a browser. Your application inherits these uses of the NSText class when it incorporates any of these objects into its interface. Your application can also create NSText objects for its own purposes.

The NSText 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 NSText 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.

. Display graphic images within its text.

. Write text to or read text from files in the form of RTFDRich Text Format files that contain TIFF or EPS images.

. 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 NSText objects.

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

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

Graphical user-interface building tools (such as Interface Builder) may give you access to NSText objects in several different configurations, such as those found in the NSTextField, NSForm, and NSScrollView objects. These classes configure an NSText object for their own specific purposes. Additionally, all NSTextFields, NSForms, NSButtons within the same windowin short, all objects that access an NSText object through associated Cellsshare the same NSText object, reducing the memory demands of an application. Thus, it's generally best to use one of these classes whenever it meets your needs, rather than create NSText objects yourself. If one of these classes doesn't provide enough flexibility for your purposes, you can create NSText objects programatically.

Plain and Rich NSText Objects

When you create an NSText object directly, by default it allows only one font, line height, text color, and paragraph format for the entire text. Once an NSText object is created, you can alter its global settings using methods such as setFont: and setTextColor:. For convenience, such an NSText object will be called a plain NSText object.

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

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

Control Word	Read	Write

\ansi yes yes

\b yes yes

\cb yes yes

\cf yes yes

\colortbl yes yes

\dnn yes yes

\fin yes yes

\fn yes yes

\fonttbl yes yes

\fsn yes yes

\i yes yes

\lin yes yes

\margrn yes yes

\paperwn yes yes

\mac yes no

\margln yes yes

\par yes yes

\pard yes no

\pca yes no

\qc yes yes

\ql yes yes

\qr yes yes

\sn yes no

\tab yes yes

\upn yes yes

NSText objects are designed to work closely with various other objects. 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, or rulertake no effort other than deciding whether the service should be enabled or disabled. The following sections discuss these interrelationships.

Notifying the NSText Object's Delegate

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






So, for example, if the delegate implements the textDidBeginEditing: method, it will receive notification upon the user's first attempt to change 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 NSText objects.

Adding Graphics to the Text

A rich NSText 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. In the programmatic approach, graphic objects are added using the replaceRange:WithRTFD: method.

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 NSText object. The NSText object automatically creates a graphic object to manage the display of the image. This feature requires a rich NSText object that has been configured to receive dragged imagessee the setImportsGraphics: method.

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 NSText object can transfer information in an RTFD document to a file and read it from a filesee the writeRTFDToFile:atomically: and readRTFDFromFile: methods.

Cooperating with Other Objects and Services

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

If an NSText 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 NSText 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.

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

Getting and Setting Contents

Managing Global Characteristics

Managing Font and Color

Managing the Selection

Sizing the Frame Rectangle

Responding to Editing Commands

Managing the Ruler



Reading and Writing RTFD Files

Managing the Field Editor

Managing the Delegate

Implemented by the Delegate