<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSText",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSText"
  },
  "title" : "NSText"
}
-->

# NSText

The most general programmatic interface for objects that manage text.

```
class NSText
```

## Overview

[`NSText`](/documentation/AppKit/NSText) draws text for user interface objects, provides text editing capabilities, and controls text attributes such as type size, font, and color.

[`NSText`](/documentation/AppKit/NSText) initialization creates an instance of a concrete subclass, such as [`NSTextView`](/documentation/AppKit/NSTextView) (generically called a text object). In general, you’re more likely to use the [`NSTextView`](/documentation/AppKit/NSTextView) subclass, because it extends the interface declared by [`NSText`](/documentation/AppKit/NSText) and provides much more sophisticated functionality than that declared in [`NSText`](/documentation/AppKit/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

### Creating a Text Object

[`-  initWithCoder:`](/documentation/AppKit/NSText/init(coder:))

[`-  initWithFrame:`](/documentation/AppKit/NSText/init(frame:))

### Getting the characters

[`string`](/documentation/AppKit/NSText/string)

The characters of the receiver’s text.

### Setting graphics attributes

[`backgroundColor`](/documentation/AppKit/NSText/backgroundColor)

The receiver’s background color to a given color.

[`drawsBackground`](/documentation/AppKit/NSText/drawsBackground)

A Boolean that controls whether the receiver draws its background.

### Setting behavioral attributes

[`editable`](/documentation/AppKit/NSText/isEditable)

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

[`selectable`](/documentation/AppKit/NSText/isSelectable)

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

[`fieldEditor`](/documentation/AppKit/NSText/isFieldEditor)

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`](/documentation/AppKit/NSText/isRichText)

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

[`importsGraphics`](/documentation/AppKit/NSText/importsGraphics)

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

### Using the Font panel and menu

[`usesFontPanel`](/documentation/AppKit/NSText/usesFontPanel)

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

### Using the ruler

[`-  toggleRuler:`](/documentation/AppKit/NSText/toggleRuler(_:))

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

[`rulerVisible`](/documentation/AppKit/NSText/isRulerVisible)

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

### Changing the selection

[`selectedRange`](/documentation/AppKit/NSText/selectedRange)

The receiver’s characters within `aRange`.

### Replacing text

[`-  replaceCharactersInRange:withRTF:`](/documentation/AppKit/NSText/replaceCharacters(in:withRTF:))

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

[`-  replaceCharactersInRange:withRTFD:`](/documentation/AppKit/NSText/replaceCharacters(in:withRTFD:))

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

[`-  replaceCharactersInRange:withString:`](/documentation/AppKit/NSText/replaceCharacters(in:with:))

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

### Action methods for editing

[`-  selectAll:`](/documentation/AppKit/NSText/selectAll(_:))

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

[`-  copy:`](/documentation/AppKit/NSText/copy(_:))

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

[`-  cut:`](/documentation/AppKit/NSText/cut(_:))

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

[`-  paste:`](/documentation/AppKit/NSText/paste(_:))

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

[`-  copyFont:`](/documentation/AppKit/NSText/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:`](/documentation/AppKit/NSText/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:`](/documentation/AppKit/NSText/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:`](/documentation/AppKit/NSText/pasteRuler(_:))

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

[`-  delete:`](/documentation/AppKit/NSText/delete(_:))

This action method deletes the selected text.

### Changing the font

[`-  changeFont:`](/documentation/AppKit/NSText/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`](/documentation/AppKit/NSText/font)

The font of all the receiver’s text.

[`-  setFont:range:`](/documentation/AppKit/NSText/setFont(_:range:))

Sets the font of characters within `aRange` to `aFont`.

### Setting text alignment

[`alignment`](/documentation/AppKit/NSText/alignment)

The alignment of all the receiver’s text.

[`-  alignCenter:`](/documentation/AppKit/NSText/alignCenter(_:))

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

[`-  alignLeft:`](/documentation/AppKit/NSText/alignLeft(_:))

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

[`-  alignRight:`](/documentation/AppKit/NSText/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`](/documentation/AppKit/NSText/textColor)

The text color of all characters in the receiver.

[`-  setTextColor:range:`](/documentation/AppKit/NSText/setTextColor(_:range:))

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

### Writing direction

[`baseWritingDirection`](/documentation/AppKit/NSText/baseWritingDirection)

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

### Setting superscripting and subscripting

[`-  superscript:`](/documentation/AppKit/NSText/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:`](/documentation/AppKit/NSText/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:`](/documentation/AppKit/NSText/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:`](/documentation/AppKit/NSText/underline(_:))

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

### Reading and writing RTF files

[`-  readRTFDFromFile:`](/documentation/AppKit/NSText/readRTFD(fromFile:))

Attempts to read the RTFD file at the specified path.

[`-  writeRTFDToFile:atomically:`](/documentation/AppKit/NSText/writeRTFD(toFile:atomically:))

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

[`-  RTFDFromRange:`](/documentation/AppKit/NSText/rtfd(from:))

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

[`-  RTFFromRange:`](/documentation/AppKit/NSText/rtf(from:))

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:`](/documentation/AppKit/NSText/checkSpelling(_:))

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

[`-  showGuessPanel:`](/documentation/AppKit/NSText/showGuessPanel(_:))

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

### Constraining size

[`maxSize`](/documentation/AppKit/NSText/maxSize)

The receiver’s maximum size.

[`minSize`](/documentation/AppKit/NSText/minSize)

The receiver’s minimum size.

[`verticallyResizable`](/documentation/AppKit/NSText/isVerticallyResizable)

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

[`horizontallyResizable`](/documentation/AppKit/NSText/isHorizontallyResizable)

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

[`-  sizeToFit`](/documentation/AppKit/NSText/sizeToFit())

Resizes the receiver to fit its text.

### Scrolling

[`-  scrollRangeToVisible:`](/documentation/AppKit/NSText/scrollRangeToVisible(_:))

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

### Setting the delegate

[`delegate`](/documentation/AppKit/NSText/delegate)

The receiver’s delegate.

### Constants

[`NSTextAlignment`](/documentation/AppKit/NSTextAlignment)

Constants that specify text alignment.

[`NSWritingDirection`](/documentation/AppKit/NSWritingDirection)

Constants that specify the writing direction.

[Movement Codes](/documentation/AppKit/movement-codes)

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

[Common Unicode Characters](/documentation/AppKit/common-unicode-characters)

### Notifications

[`NSTextDidBeginEditingNotification`](/documentation/AppKit/NSText/didBeginEditingNotification)

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

[`NSTextDidChangeNotification`](/documentation/AppKit/NSText/didChangeNotification)

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

[`NSTextDidEndEditingNotification`](/documentation/AppKit/NSText/didEndEditingNotification)

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

[`NSTextMovementUserInfoKey`](/documentation/AppKit/NSText/movementUserInfoKey)

The `userInfo` dictionary key for the [`didEndEditingNotification`](/documentation/AppKit/NSText/didEndEditingNotification) notification.

[`NSTextMovement`](/documentation/AppKit/NSTextMovement)

## Relationships

### Conforms To

[`NSStandardKeyBindingResponding`](/documentation/AppKit/NSStandardKeyBindingResponding)

[`NSAnimatablePropertyContainer`](/documentation/AppKit/NSAnimatablePropertyContainer)

[`NSAccessibilityProtocol`](/documentation/AppKit/NSAccessibilityProtocol)

[`NSTouchBarProvider`](/documentation/AppKit/NSTouchBarProvider)

[`NSDraggingDestination`](/documentation/AppKit/NSDraggingDestination)

[`NSChangeSpelling`](/documentation/AppKit/NSChangeSpelling)

[`NSUserInterfaceItemIdentification`](/documentation/AppKit/NSUserInterfaceItemIdentification)

[`NSAppearanceCustomization`](/documentation/AppKit/NSAppearanceCustomization)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSAccessibilityElementProtocol`](/documentation/AppKit/NSAccessibilityElementProtocol)

[`NSUserActivityRestoring`](/documentation/AppKit/NSUserActivityRestoring)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`Equatable`](/documentation/Swift/Equatable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSIgnoreMisspelledWords`](/documentation/AppKit/NSIgnoreMisspelledWords)

[`Hashable`](/documentation/Swift/Hashable)

[`Sendable`](/documentation/Swift/Sendable)

### Inherited By

[`NSTextView`](/documentation/AppKit/NSTextView)

### Inherits From

[`NSView`](/documentation/AppKit/NSView)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)