 |
 |
Some NSTextView questions...
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
I'm new to Cocoa, and I'm new to Objective C, although I've used C and C++. After reading through Apple's documentation I've figured out how to take a value from a text field as an int or string or something and do stuff with it, like how to put text or numbers into a field, and similar stuff. However, I think that I don't quite understand a fundamental part of putting text into a text field. First, in NSTextView, I don't understand how I know where new text will be inserted, or a really good way to insert text. I had thought that insertText: would insert text right after I had put text before and it does... unless someone has selected text before that. Then it overwrites the text that is selected. Also, as is mentioned in another thread here, I don't know how to add text without setEditable:YES , and I read the suggestion about turning it on, making the changes, and then turning it off, but that doesn't help me if someone has selected the text and I want to leave the ability to select text on. Basically I want to know how I know where I am in a text field, and be able to tell it where to add text without deselecting what the user has selected or overwriting anything. What I want to do is display messages kind of like Disk Utility does when you verify or repair a disk. Any suggestions will be appreciated.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
First of all, there is NSTextView, which is a multiline, rich text editor; then there's NSTextField, which is usually a single line, monofont text control. The former is usually used for TextEdit-style documents, and the latter for input fields. Because the latter is a subclass of NSControl, it also plays the target/action game (so you can make connections to other objects and send an action when the user edits the text). Both of these are available in InterfaceBuilder's default Cocoa palette.
Anyway, assuming you are using NSTextView, and not NSTextField:
The insertText: method emulates the insertion of text by the user, replacing the selection. As a result, as you noticed, it won't work when the text is not editable and it's not super flexible in how you specify which text to replace.
The methods you want to use are the ones in NSText (the superclass of NSTextView), such as:
- (void)replaceCharactersInRange  NSRange)range
withString  NSString *)aString;
This will replace the characters in the specified range with the contents of the specified string. Sounds like it might do what you need.
For even more sophisticated text editing, you can go to the backing store of the NSTextView; call -textStorage to get its NSTextStorage, which you can modify with NSMutableAttributedString methods. However, then you are bypassing the text view's undo, delegation, and editing mechanisms unless you take some extra care. See the replaceAll: method in TextEdit's TextFinder.m source file for example of how to edit the text in an NSTextView by going through its text storage.
Ali
[This message has been edited by ali (edited 12-24-2000).]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Thanks. I would have never thought to use a method with replace in it's name in order to insert new text. It took me a while to figure out how to use NSRange. It's strange. On the documentation that came with the developer tools CD it says that NSRange is a structure; that's how I used it, but on Apple's web site it says that NSRange is a class. So... is there an NSRange class, did Apple get rid of it as a structure in some newer release of the developer tools or something?
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
Thanks. I would have never thought to use a method with replace in it's name in order to insert new text.
replace is used in various classes (NSString, NSData, NSArray) to denote replacing a range of items with other items, that's why it makes sense in this context.
On the documentation that came with the developer tools CD it says that NSRange is a structure; that's how I used it, but on Apple's web site it says that NSRange is a class. So... is there an NSRange class, did Apple get rid of it as a structure in some newer release of the developer tools or something?
NSRange in Objective-C is a simple open struct. There are a few functions to manipulate it, but you can also access the fields directly. In Java, where there are no structs, NSRange is a class. That might explain the "discrepancy" you noted.
Note that the NSRange struct in Objective-C and the NSRange class in Java are equivalent in the sense that they get changed back and forth automatically across the Java-ObjC bridge.
Ali
[This message has been edited by ali (edited 12-26-2000).]
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|