The NSScrollView and NSTextView Classes

In Chapter 10 we introduced the NSScrollView and NSTextView classes when we dragged an NSScrollView object from IB’s Cocoa-Data palette into a MathPaper window. At the time, we said that the NSScrollView object “contains” an NSTextView object. Perhaps a better way of putting it is to say that an NSTextView is “embedded” inside each NSScrollView. The NSScrollView object in IB’s Palettes window actually contains the following nine objects:

NSScrollView

Displays the scroller and does the actual scrolling

NSTextView

Displays the text

NSClipView

Helps arrange the communication between the NSScrollView and the NSTextView

Vertical scroller

Controls up-down scrolling and shows where you are in the document

Horizontal scroller

Controls left-right scrolling and shows where you are in the document

NSTextStorage object

Holds the data that the NSTextView displays

NSTextContainer object

Defines the region where the text will be displayed

NSLayoutManager object

Controls the layout of the NSTextStorage object’s information within the NSTextView

NSSimpleHorizontalTypesetter

Does the typesetting

You can control whether each scroller is displayed by sending the setHasVerticalScroller: or setHasHorizontalScroller: messages (with the arguments YES or NO) to the NSScrollView. By default, the NSScrollView that you drag off IB’s Cocoa-Data palette displays the vertical scroller, but not the horizontal one.

NSTextView objects are most frequently used with NSScrollView objects, which is why Interface Builder provides them that way. We’ll learn other ways to use NSScrollViews in the GraphPaper application later in this book.

The NSTextView object is Cocoa’s general-purpose text editor. An NSTextView object is used by almost every application that allows text entry. For example, Mac OS X’s TextEdit text editor uses the NSTextView class as its main editing tool. It is also used by many commercial drawing applications, such as Stone Design’s Create.

An NSTextView object can do all of the following:

  • Display single-font (monofont) text or multiple-font text

  • Automatically word-wrap

  • Work with the pasteboard

  • Save its contents in a stream of ASCII, Unicode, or RTF text

The NSText class supports a general programmatic interface for objects that manage text. However, you typically use instances of its subclass, NSTextView.

Every NSWindow object has a special NSText object called the field editor that can be assigned minor editing tasks for the NSWindow. This NSText object is shared among NSForm, NSMatrix, NSBrowser, and NSTextField objects located within a single associated on-screen window. When you are working with text in one of these objects in a window, the field editor reads in the text and lets you edit it. When you’re done, the field editor NSText object spits out its contents and puts the text back into the appropriate location. The shuttling about of the field editor is all fairly transparent and is handled automatically by these classes.

NSTextView Class Basics

Like objects of the NSApplication and NSWindow classes, an NSTextView object can have a delegate object. You can use an NSTextView’s delegate to find out when the user has made changes to the text or to prevent changes from happening under certain circumstances, as described in Table 11-1.

Table 11-1. Common delegate methods of the NSText and NSTextView classes

Delegate method

Purpose

-(void)textDidBeginEditing: (NSNotification *)aNotification

Alerts the delegate that the user has started to edit the NSText(View) object.

-(void)textDidChange: (NSNotification *)aNotification

Alerts the delegate that the text or formatting of the NSText(View) object has changed.

-(void)textDidEndEditing: (NSNotification *)aNotification

Alerts the delegate that the user is finished editing the NSText(View) object. This message is usually sent when the user clicks elsewhere on the window.

- (BOOL)textShouldBeginEditing: (NSText *)aTextObject

Requests permission from the delegate for the user to edit the text contained within the NSText(View) object. If the delegate returns YES, editing is allowed. If the delegate returns NO, editing is not allowed.

- (BOOL)textShouldEndEditing: (NSText *)aTextObject

Requests permission from the delegate to allow the user to end editing. The delegate can use this method to take the opportunity to validate the contents of the NSText(View) object, and to force the user to change an invalid value to a valid value before allowing the user to do something else inside the application.

Most often, you’ll use an NSTextView object to display a chunk of text for the user (e.g., the contents of an article or a mail message). Alternatively, you might use an NSTextView object to let the user enter some free-form text (again, such as a mail message). If you’re writing a full-featured text editor, the NSTextView object is a great place to start. With its architecture, you can easily create an elaborate system that supports multiple columns of text, runarounds, and many other features.

When it runs as part of an application, the NSTextView object contains a copy of all of the text that you are editing. The more text that you have, the longer it will take to load the text into the NSTextView object and to display it for the first time. However, the NSTextView object collection is still extremely fast — a lot of very smart people have been working for many years on these objects. Give them a try before you try to do better.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.39.93