< Previous PageNext Page > Hide TOC

Creating an NSTextView Programmatically

At times, you may need to assemble the text system programmatically. You can do this in either of two ways: by creating an NSTextView object and letting it create its network of supporting objects or by building the network of objects yourself. In most cases, you’ll find it sufficient to create an NSTextView object and let it create the underlying network of text-handling objects, as discussed in this article. If your application has complex text-layout requirements, you’ll have to create the network yourself; see “Assembling the Text System by Hand” for information.

You create an NSTextView object programmatically in the usual way: by sending the alloc and init... messages.

You can also create NSTextView objects using one of its constructors in Java or either of these methods in Objective-C:

The method that takes one argument, initWithFrame:, is the simplest way to obtain an NSTextView object—it creates all the other components of the text system for you and releases them when you’re done. If you use the method that takes two arguments, initWithFrame:textContainer:, you must create (and release) the other components yourself.

Listing 1 shows how you can create an NSTextView object, given an NSWindow object represented by aWindow.

Listing 1  Creating an NSTextView programmatically

/* determine the size for the NSTextView */
NSRect cFrame =[[aWindow contentView] frame];
 
/* create the NSTextView and add it to the window */
NSTextView *theTextView = [[NSTextView alloc] initWithFrame:cFrame];
[aWindow setContentView:theTextView];
[aWindow makeKeyAndOrderFront:nil];
[aWindow makeFirstResponder:theTextView];

This code determines the size for the NSTextView’s frame rectangle by asking aWindow for the size of its content view. The NSTextView is then created and made the content view of aWindow using setContentView:. Finally, the makeKeyAndOrderFront: and makeFirstResponder: messages display the window and cause theTextView to prepare to accept keyboard input.

NSTextView’s initWithFrame: method not only initializes the receiving NSTextView object, it causes the object to create and interconnect the other components of the text system. This is a convenience that frees you from having to create and interconnect them yourself. Since the NSTextView created these supporting objects, it’s responsible for releasing them when they are no longer needed. When you’re done with the NSTextView, release it and it takes care of releasing the other objects of the text system. Note that this ownership policy is in effect only if you let NSTextView create the components of the text system. See “Assembling the Text System by Hand” for more information on object ownership when you create the components yourself.



< Previous PageNext Page > Hide TOC


© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-06-28)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.