Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Implements | |
Package | com.apple.cocoa.application |
Companion guide |
NSText declares the most general programmatic interface for objects that manage text. You usually use instances of its subclass, NSTextView.
readRTFDFromFile
Reads an .rtf
or .rtfd
file.
writeRTFDToFile
Writes the receiver’s text to a file.
string
Returns the receiver’s text without attributes.
RTFFromRange
Returns the receiver’s text with attributes.
RTFDFromRange
Returns the receiver’s text with attributes and attachments.
The NSText class declares the most general programmatic interface to objects that manage text. Cocoa offers a subclass of NSText, NSTextView, that extends the interface declared by NSText and provides much more sophisticated functionality than that declared in NSText.
NSText’s constructor creates an instance of a concrete subclass, such as NSTextView. Instances of any of these classes are generically called text objects.
Text objects are used by the Application Kit wherever text appears in interface objects: 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 application can also create text objects for its own purposes.
setEditable
isEditable
setSelectable
isSelectable
setFieldEditor
isFieldEditor
setRichText
isRichText
setImportsGraphics
importsGraphics
setMaxSize
maxSize
setMinSize
minSize
setVerticallyResizable
isVerticallyResizable
setHorizontallyResizable
isHorizontallyResizable
sizeToFit
textShouldBeginEditing
delegate method
textDidBeginEditing
delegate method
textShouldEndEditing
delegate method
textDidEndEditing
delegate method
textDidChange
delegate method
Creates an NSText with a zero-sized frame rectangle.
public NSText
()
Creates a text object with frameRect as its frame rectangle.
public NSText
(NSRect frameRect)
This method actually substitutes an instance of a concrete subclass of NSText, such as NSTextView, and configures that instance to archive itself.
This action method applies center alignment to selected paragraphs (or all text if the receiver is a plain text object).
public void alignCenter
(Object sender)
This action method applies left alignment to selected paragraphs (or all text if the receiver is a plain text object).
public void alignLeft
(Object sender)
Returns the alignment of the first selected paragraph, or of all text for a plain text object.
public int alignment
()
This value is one of the alignments described in “Constants.”
Text using NaturalTextAlignment
is actually displayed using one of the other alignments, depending on the natural alignment of the text’s script.
This action method applies right alignment to selected paragraphs (or all text if the receiver is a plain text object).
public void alignRight
(Object sender)
Returns the receiver’s background color.
public NSColor backgroundColor
()
This action method changes the font of the selection for a rich text object, or of all text for a plain text object.
public void changeFont
(Object sender)
If the receiver doesn’t use the Font panel, this method does nothing.
This method changes the font by sending a convertFont
message to the shared NSFontManager and applying each NSFont returned to the appropriate text. See the NSFontManager class specification for more information on font conversion.
Replaces the selected word in the receiver with a corrected version from the Spelling panel.
public void changeSpelling
(Object sender)
This message is sent by the NSSpellChecker to the object whose text is being checked. To get the corrected spelling, ask sender for the string value of its selected cell (visible to the user as the text field in the Spelling panel). This method should replace the selected portion of the text with the string that it gets from the NSSpellChecker.
This action method searches for a misspelled word in the receiver’s text.
public void checkSpelling
(Object sender)
The search starts at the end of the selection and continues until it reaches a word suspected of being misspelled or the end of the text. If a word isn’t recognized by the spelling server, a showGuessPanel
message then opens the Guess panel and allows the user to make a correction or add the word to the local dictionary.
This action method copies the selected text onto the general pasteboard, in as many formats as the receiver supports.
public void copy
(Object sender)
A plain text object uses NSPasteboard.StringPboardType
for plain text, and a rich text object also uses NSPasteboard.RTFPboardType
.
This action method copies the font information for the first character of the selection (or for the insertion point) onto the font pasteboard, as NSPasteboard.FontPboardType
.
public void copyFont
(Object sender)
This action method copies the paragraph style information for first selected paragraph onto the ruler pasteboard, as NSPasteboard.RulerPboardType
, and expands the selection to paragraph boundaries.
public void copyRuler
(Object sender)
This action method deletes the selected text and places it onto the general pasteboard, in as many formats as the receiver supports.
public void cut
(Object sender)
A plain text object uses NSPasteboard.StringPboardType
for plain text, and a rich text object also uses NSPasteboard.RTFPboardType
.
Returns the receiver’s delegate, or null
if it has none.
public Object delegate
()
This action method deletes the selected text.
public void delete
(Object sender)
Returns true
if the receiver draws its background, false
if it doesn’t.
public boolean drawsBackground
()
Returns the font of the first character in the receiver’s text, or of the insertion point if there’s no text.
public NSFont font
()
This action method informs the receiver to ignore misspelled words on a document-by-document basis. This method is sent by the NSSpellChecker instance.
public void ignoreSpelling
(Object sender)
Returns true
if the receiver allows the user to import files by dragging, false
if it doesn’t.
public boolean importsGraphics
()
A text object that accepts dragged files is also a rich text object.
Returns true
if the receiver allows the user to edit text, false
if it doesn’t.
public boolean isEditable
()
You can change the receiver’s text programmatically regardless of this setting.
If the receiver is editable, it’s also selectable.
Returns true
if the receiver interprets Tab, Shift-Tab, and Return (Enter) as cues to end editing and possibly to change the first responder; false
if it accepts them as text input.
public boolean isFieldEditor
()
See the NSWindow class specification for more information on field editors. By default, NSText objects don’t behave as field editors.
Returns true
if the receiver automatically changes its width to accommodate the width of its text, false
if it doesn’t.
public boolean isHorizontallyResizable
()
By default, an NSText object is not horizontally resizable.
Returns true
if the receiver allows the user to apply attributes to specific ranges of the text, false
if it doesn’t.
public boolean isRichText
()
Returns true
if the receiver’s enclosing scroll view shows its ruler, false
otherwise.
public boolean isRulerVisible
()
Returns true
if the receiver allows the user to select text, false
if it doesn’t.
public boolean isSelectable
()
Returns true
if the receiver automatically changes its height to accommodate the height of its text, false
if it doesn’t.
public boolean isVerticallyResizable
()
By default, an NSText object is vertically resizable.
Returns the receiver’s maximum size.
public NSSize maxSize
()
Returns the receiver’s minimum size.
public NSSize minSize
()
This action method pastes text from the general pasteboard at the insertion point or over the selection.
public void paste
(Object sender)
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.
public void pasteFont
(Object sender)
This action method pastes paragraph style information from the ruler pasteboard onto the selected paragraphs of a rich text object.
public void pasteRuler
(Object sender)
It doesn’t apply to a plain text object.
Attempts to read the RTFD file at path, returning true
if successful and false
if not.
public boolean readRTFDFromFile
(String path)
path should be the path for an .rtf
file or an .rtfd
file wrapper, not for the RTF file within an .rtfd
file wrapper.
Replaces the characters in aRange with aString.
public void replaceCharactersInRange
(NSRange aRange, String aString)
For a rich text object, the text of aString is assigned the formatting attributes of the first character of the text it replaces, or of the character immediately before aRange if the range’s length is 0. If the range’s location is 0, the formatting attributes of the first character in the receiver are used.
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Replaces the characters in aRange with RTF text interpreted from rtfData.
public void replaceCharactersInRangeWithRTF
(NSRange aRange, NSData rtfData)
This method applies only to rich text objects.
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Replaces the characters in aRange with RTFD text interpreted from rtfdData.
public void replaceCharactersInRangeWithRTFD
(NSRange aRange, NSData rtfdData)
This method applies only to rich text objects.
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Returns an NSData object that contains an RTFD stream corresponding to the characters and attributes within aRange.
public NSData RTFDFromRange
(NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
When writing data to the pasteboard, you can use the NSData object as the first argument to NSPasteboard’s setDataForType
method, with a second argument of NSPasteboard.RTFDPboardType
.
Returns an NSData object that contains an RTF stream corresponding to the characters and attributes within aRange, omitting any attachment characters and attributes.
public NSData RTFFromRange
(NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
When writing data to the pasteboard, you can use the NSData object as the first argument to NSPasteboard’s setDataForType
method, with a second argument of NSPasteboard.RTFPboardType
.
Scrolls the receiver in its enclosing scroll view so the first characters of aRange are visible.
public void scrollRangeToVisible
(NSRange aRange)
This action method selects all of the receiver’s text.
public void selectAll
(Object sender)
Returns the range of selected characters.
public NSRange selectedRange
()
Sets the alignment of all the receiver’s text to mode.
public void setAlignment
(int mode)
mode may be one of the alignments described in “Constants.”
Text using NaturalTextAlignment
is actually displayed using one of the other alignments, depending on the natural alignment of the text’s script.
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Sets the receiver’s background color to aColor.
public void setBackgroundColor
(NSColor aColor)
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Sets the receiver’s delegate to anObject, without retaining it.
public void setDelegate
(Object anObject)
Controls whether the receiver draws its background.
public void setDrawsBackground
(boolean flag)
If flag is true
, the receiver fills its background with the background color; if flag is false
, it doesn’t.
Controls whether the receiver allows the user to edit its text.
public void setEditable
(boolean flag)
If flag is true
, the receiver allows the user to edit text and attributes; if flag is false
, it doesn’t. You can change the receiver’s text programmatically regardless of this setting. If the receiver is made editable, it’s also made selectable. NSText objects are by default editable.
Controls whether the receiver interprets Tab, Shift-Tab, and Return (Enter) as cues to end editing and possibly to change the first responder.
public void setFieldEditor
(boolean flag)
If flag is true
, it does; if flag is false
, it doesn’t, instead accepting these characters as text input. See the NSWindow class specification for more information on field editors. By default, NSText objects don’t behave as field editors.
Sets the font of all the receiver’s text to aFont.
public void setFont
(NSFont aFont)
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Sets the font of characters within aRange to aFont.
public void setFontInRange
(NSFont aFont, NSRange aRange)
This method applies only to a rich text object.
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Controls whether the receiver changes its width to fit the width of its text.
public void setHorizontallyResizable
(boolean flag)
If flag is true
it does; if flag is false
it doesn’t.
Controls whether the receiver allows the user to import files by dragging.
public void setImportsGraphics
(boolean flag)
If flag is true
, it does; if flag is false
, it doesn’t. If the receiver is set to accept dragged files, it’s also made a rich text object. Subclasses may or may not accept dragged files by default.
Sets the receiver’s maximum size to aSize.
public void setMaxSize
(NSSize aSize)
Sets the receiver’s minimum size to aSize.
public void setMinSize
(NSSize aSize)
Controls whether the receiver allows the user to apply attributes to specific ranges of the text.
public void setRichText
(boolean flag)
If flag is true
it does; if flag is false
it doesn’t. If flag is false
, the receiver is also set not to accept dragged files. Subclasses may or may not let the user apply multiple attributes to the text and accept drag files by default.
Controls whether the receiver allows the user to select its text.
public void setSelectable
(boolean flag)
If flag is true
, the receiver allows the user to select text; if flag is false
, it doesn’t. You can set selections programmatically regardless of this setting. If the receiver is made not selectable, it’s also made not editable. NSText objects are by default editable and selectable.
Selects the receiver’s characters within aRange.
public void setSelectedRange
(NSRange aRange)
Replaces the receiver’s entire text with aString, applying the formatting attributes of the old first character to its new contents.
public void setString
(String aString)
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Sets the text color of all characters in the receiver to aColor.
public void setTextColor
(NSColor aColor)
Removes the text color attribute if aColor is null
.
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Sets the text color of characters within aRange to aColor.
public void setTextColorInRange
(NSColor aColor, NSRange aRange)
Removes the text color attribute if aColor is null
. This method applies only to rich text objects.
This method does not include undo support by default. Clients must invoke shouldChangeTextInRanges
or shouldChangeTextInRange
to include this method in an undoable action.
Controls whether the receiver uses the Font panel and Font menu.
public void setUsesFontPanel
(boolean flag)
If flag is true
, the receiver responds to messages from the Font panel and from the Font menu and updates the Font panel with the selection font whenever it changes. If flag is false
the receiver doesn’t do any of these actions. By default, an NSText object uses the Font panel and menu.
Controls whether the receiver changes its height to fit the height of its text.
public void setVerticallyResizable
(boolean flag)
If flag is true
it does; if flag is false
it doesn’t.
This action method opens the Spelling panel, allowing the user to make a correction during spell checking.
public void showGuessPanel
(Object sender)
Resizes the receiver to fit its text.
public void sizeToFit
()
The text view will not be sized any smaller than its minimum size, however.
Returns the characters of the receiver’s text.
public String string
()
For performance reasons, this method returns the current backing store of the text object. If you want to maintain a snapshot of this as you manipulate the text storage, you should make a copy of the appropriate substring.
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.
public void subscript
(Object sender)
subscript
unscript
lowerBaseline
(NSTextView)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.
public void superscript
(Object sender)
subscript
unscript
raiseBaseline
(NSTextView)Returns the color of the receiver’s first character, or for the insertion point if there’s no text.
public NSColor textColor
()
This action method shows or hides the ruler, if the receiver is enclosed in a scroll view.
public void toggleRuler
(Object sender)
This action method underlines selected text for a rich text object, or all text for a plain text object.
public void underline
(Object sender)
This action method removes any superscripting or subscripting from selected text (or all text if the receiver is a plain text object).
public void unscript
(Object sender)
subscript
superscript
raiseBaseline
(NSTextView)lowerBaseline
(NSTextView)Returns true
if the receiver uses the Font panel, false
otherwise.
public boolean usesFontPanel
()
Writes the receiver’s text as RTF with attachments to a file or directory at path.
public boolean writeRTFDToFile
(String path, boolean atomicFlag)
Returns true
on success and false
on failure. If atomicFlag is true
, attempts to write the file safely so that an existing file at path is not overwritten, nor does a new file at path actually get created, unless the write is successful.
These constants specify text alignment:
These constants specify the reason for a change of editing focus among text fields, in essence answering the question “why am I leaving the field?” They are the possible values for the TextMovement
key of the TextDidEndEditingNotification
userInfo
dictionary. The field editor makes sure that these are the values sent when the user presses the Tab, Backtab, or Return key while editing. The control then uses this information to decide where to send focus next.
These constants specify several commonly used Unicode characters.
Informs the delegate that the text object has begun editing (that the user has begun changing it).
public abstract void textDidBeginEditing
(NSNotification aNotification)
The name of aNotification is TextDidBeginEditingNotification
.
Informs the delegate that the text object has changed its characters or formatting attributes.
public abstract void textDidChange
(NSNotification aNotification)
The name of aNotification is TextDidChangeNotification
.
Informs the delegate that the text object has finished editing (that it has resigned first responder status).
public abstract void textDidEndEditing
(NSNotification aNotification)
The name of aNotification is TextDidEndEditingNotification
.
Invoked from a text object’s implementation of becomeFirstResponder
, this method requests permission for aTextObject to begin editing.
public abstract boolean textShouldBeginEditing
(NSText aTextObject)
If the delegate returns true
, the text object proceeds to make changes. If the delegate returns false
, the text object abandons the editing operation. This method is invoked whenever aTextObject attempts to become first responder. It is also invoked when the user drags and drops a file onto the text object.
makeFirstResponder
(NSWindow)becomeFirstResponder
(NSResponder)Invoked from a text object’s implementation of resignFirstResponder
, this method requests permission for aTextObject to end editing.
public abstract boolean textShouldEndEditing
(NSText aTextObject)
If the delegate returns true
, the text object proceeds to finish editing and resign first responder status. If the delegate returns false
, the text object selects all of its text and remains the first responder.
resignFirstResponder
(NSResponder)Posted when an NSText object begins any operation that changes characters or formatting attributes.
The notification object is the notifying NSText object. This notification does not contain a userInfo dictionary.
Posted after an NSText object performs any operation that changes characters or formatting attributes.
The notification object is the notifying NSText object. This notification does not contain a userInfo dictionary.
Posted when an NSText object completes any operation that changes characters or formatting attributes.
The notification object is the notifying NSText object. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
Possible movement code values are described in “Constants.” |
© 1997, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-02-01)