Class

NSText

The most general programmatic interface for objects that manage text.

Declaration

@interface NSText : NSView

Overview

NSText draws text for user interface objects, provides text editing capabilities, and controls text attributes such as type size, font, and color.

NSText initialization creates an instance of a concrete subclass, such as NSTextView (generically called a text object). In general, you're more likely to use the NSTextView subclass, because it extends the interface declared by NSText and provides much more sophisticated functionality than that declared in NSText.

AppKit uses text objects wherever text appears in interface objects. For example, a text object draws the title of a window, the commands in a menu, the title of a button, and the items in a browser. Your app can also create text objects for its own purposes.

Topics

Getting the characters

string

The characters of the receiver’s text.

Setting graphics attributes

backgroundColor

The receiver’s background color to a given color.

drawsBackground

A Boolean that controls whether the receiver draws its background.

Setting behavioral attributes

editable

A Boolean that controls whether the receiver allows the user to edit its text.

selectable

A Boolean that controls whether the receiver allows the user to select its text.

fieldEditor

A Boolean that controls whether the receiver interprets Tab, Shift-Tab, and Return (Enter) as cues to end editing and possibly to change the first responder.

richText

A Boolean that controls whether the receiver allows the user to apply attributes to specific ranges of the text.

importsGraphics

A Boolean that controls whether the receiver allows the user to import files by dragging.

Using the Font panel and menu

usesFontPanel

A Boolean that controls whether the receiver uses the Font panel and Font menu.

Using the ruler

- toggleRuler:

This action method shows or hides the ruler, if the receiver is enclosed in a scroll view.

rulerVisible

A Boolean value that indicates whether the receiver’s enclosing scroll view shows its ruler.

Changing the selection

selectedRange

The receiver’s characters within aRange.

Replacing text

- replaceCharactersInRange:withRTF:

Replaces the characters in the given range with RTF text interpreted from the given RTF data.

- replaceCharactersInRange:withRTFD:

Replaces the characters in the given range with RTFD text interpreted from the given RTFD data.

- replaceCharactersInRange:withString:

Replaces the characters in the given range with those in the given string.

Action methods for editing

- selectAll:

This action method selects all of the receiver’s text.

- copy:

This action method copies the selected text onto the general pasteboard, in as many formats as the receiver supports.

- cut:

This action method deletes the selected text and places it onto the general pasteboard, in as many formats as the receiver supports.

- paste:

This action method pastes text from the general pasteboard at the insertion point or over the selection.

- copyFont:

This action method copies the font information for the first character of the selection (or for the insertion point) onto the font pasteboard, as NSFontPboardType.

- pasteFont:

This action method pastes font information from the font pasteboard onto the selected text or insertion point of a rich text object, or over all text of a plain text object.

- copyRuler:

This action method copies the paragraph style information for first selected paragraph onto the ruler pasteboard, as NSRulerPboardType, and expands the selection to paragraph boundaries.

- pasteRuler:

This action method pastes paragraph style information from the ruler pasteboard onto the selected paragraphs of a rich text object.

- delete:

This action method deletes the selected text.

Changing the font

- changeFont:

This action method changes the font of the selection for a rich text object, or of all text for a plain text object.

font

The font of all the receiver’s text.

- setFont:range:

Sets the font of characters within aRange to aFont.

Setting text alignment

alignment

The alignment of all the receiver’s text.

- alignCenter:

This action method applies center alignment to selected paragraphs (or all text if the receiver is a plain text object).

- alignLeft:

This action method applies left alignment to selected paragraphs (or all text if the receiver is a plain text object).

- alignRight:

This action method applies right alignment to selected paragraphs (or all text if the receiver is a plain text object).

Setting text color

textColor

The text color of all characters in the receiver.

- setTextColor:range:

Sets the text color of characters within the specified range to the specified color.

Writing direction

baseWritingDirection

The initial writing direction used to determine the actual writing direction for text.

Setting superscripting and subscripting

- superscript:

This action method applies a superscript attribute to selected text (or all text if the receiver is a plain text object), raising its baseline offset by a predefined amount.

- subscript:

This action method applies a subscript attribute to selected text (or all text if the receiver is a plain text object), lowering its baseline offset by a predefined amount.

- unscript:

This action method removes any superscripting or subscripting from selected text (or all text if the receiver is a plain text object).

Underlining text

- underline:

Adds the underline attribute to the selected text attributes if absent; removes the attribute if present.

Reading and writing RTF files

- readRTFDFromFile:

Attempts to read the RTFD file at path, returning YES if successful and NO if not.

- writeRTFDToFile:atomically:

Writes the receiver’s text as RTF with attachments to a file or directory at path.

- RTFDFromRange:

Returns an NSData object that contains an RTFD stream corresponding to the characters and attributes within aRange.

- RTFFromRange:

Returns an NSData object that contains an RTF stream corresponding to the characters and attributes within aRange, omitting any attachment characters and attributes.

Checking spelling

- checkSpelling:

This action method searches for a misspelled word in the receiver’s text.

- showGuessPanel:

This action method opens the Spelling panel, allowing the user to make a correction during spell checking.

Constraining size

maxSize

The receiver’s maximum size.

minSize

The receiver’s minimum size.

verticallyResizable

A Boolean that controls whether the receiver changes its height to fit the height of its text.

horizontallyResizable

A Boolean that controls whether the receiver changes its width to fit the width of its text.

- sizeToFit

Resizes the receiver to fit its text.

Scrolling

- scrollRangeToVisible:

Scrolls the receiver in its enclosing scroll view so the first characters of aRange are visible.

Setting the delegate

delegate

The receiver’s delegate.

Constants

NSTextAlignment

These constants specify text alignment.

NSWritingDirection

These constants specify the writing directions:

Movement Codes

The reason for a change of editing focus among text fields.

Notifications

NSTextDidBeginEditingNotification

Posted when an NSText object begins any operation that changes characters or formatting attributes.

NSTextDidChangeNotification

Posted after an NSText object performs any operation that changes characters or formatting attributes.

NSTextDidEndEditingNotification

Posted when focus leaves an NSText object, whether or not any operation has changed characters or formatting attributes.

NSTextMovementUserInfoKey

The userInfo dictionary key for the NSTextDidEndEditingNotification notification.

Relationships

Inherits From

See Also

Text Views

NSTextField

Text that the user can select or edit and that sends its action message to its target when the user presses the Return key.

NSTextFieldDelegate

A protocol that a text field delegate can use to control its field editor action menu.

NSTextView

A view that draws text and handles user interactions with that text.

NSTextViewDelegate

A set of optional methods that text view delegates can use to manage selection, set text attributes, work with the spell checker, and more.